home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / 3dvect39 / dxf23dv / dxf23dv.asm < prev    next >
Assembly Source File  |  1994-10-30  |  83KB  |  3,163 lines

  1. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2. ;
  3. ; DXF to 3DVectors Converter by John McCarthy (Also .PLG converter)
  4. ;
  5. ; Current DXF types supported: 3DFACE, PFACE
  6. ;
  7. ; The 3dfaces are between 1 and 4 points where:
  8. ;   1 = single point
  9. ;   2 = line
  10. ;   3 = triangle
  11. ;   4 = quadagon (?)
  12. ;
  13. ; Whereas the PFACE performs the same function but can be a mesh up to:
  14. ; 1 to 3000 vertexes    (see maxpolyline below)
  15. ; 1 to 2000 connections (see maxconns below)
  16. ; 1 to 3000 surfaces    (see maxsurfs below)
  17. ;
  18. ;Options:
  19. ;
  20. ;DXF23DV inputname outputname [-s# -x# -y# -z# -mfilename -u# -v# -w# -l -n -q]
  21. ;
  22. ; -x  x translation for object (before scale) - can be floating point, +z = up
  23. ; -y  y translation for object (before scale) - can be floating point, +z = up
  24. ; -z  z translation for object (before scale) - can be floating point, +z = up
  25. ; -s  scale factor                            - can be floating point
  26. ; -u  x translation for object (after scale)  - integer only, +y = down
  27. ; -v  y translation for object (after scale)  - integer only, +y = down
  28. ; -w  z translation for object (after scale)  - integer only, +y = down
  29. ; -m  materials list filename (corresponds to layer names)
  30. ; -l  selective layer processing (only process layers that are found
  31. ;     in materials file)
  32. ; -n  output true calculated surface normal (otherwise 0,0,0), useless option
  33. ; -q  negate Y output axis
  34. ; -b  sort surfaces based on surface normal (default=sort)
  35. ;
  36. ; Material names can be as long as ACAD allows!
  37. ;
  38. ; I you have trouble assigning a material to a line it  may  be  because  the
  39. ; layer that the line is on  has  the  shading  option.  Lines  do  not  have
  40. ; surface  normals  and  therefore   cannot  have  the  shading  option  set.
  41. ; If DXF23DV finds this occurance, it will insert   a  default  texture  that
  42. ; has no shading texture.  eg:  0,0,colour,0
  43. ;
  44. ; If your a total  knumbskull  (like  me)  and  you  get  ACAD's  co-ordinate
  45. ; system messed up (like me) you can use  the  -q  option  to  negate  the  Y
  46. ; axis and reverse the  orientation  of  the  polygons.  This  will  fix  the
  47. ; object if it appears to be "backwards".
  48. ;
  49. ; Do not use punctuation in your material names or layer names.
  50. ; A # sign in the materials means a comment
  51. ; A : means to put the following text AFTER the connection data.
  52. ;
  53. ; Using the PFACE allows ACAD to assign polygons with more  than  4  surfaces.
  54. ; This removes the need for polygon optimization  and  gives  greater  control
  55. ; over the implementation of surfaces.
  56. ;
  57. ; The ACAD layer name is matched up with a materials file to give each surface
  58. ; a seperate surface type/colour/whatever.  Surfaces can be  selected  through
  59. ; AutoCAD to be double sided, shaded, sine waved, or anything the  user  wants
  60. ; simply by nameing a new layer and assigning those  surfaces  to  that layer.
  61. ; The materials file can then be edited/added in order to obtain  the  results
  62. ; the user desires.  If  the  shade  option  is  used,  the  words  0,0,0  are
  63. ; AUTOMATICALLY inserted into the face.  There is no need  to  use  the  colon
  64. ; option to add them.  For a rundown on the colon option, see version 0.3
  65. ;
  66. ; Sample materials file follows:
  67. ;
  68. ;  # This is a comment
  69. ;  # There must be 5 fields in each assigned texture!!
  70. ;
  71. ;  CONSTANT     0,0,0,colour0,0
  72. ;  SHADED       0,shade,0,colour1,0
  73. ;  SINE         0,wavey,0,colour2+rnd/16,0
  74. ;  BOTHSIDES    both,0,0,colour3,0
  75. ;  BOTHSHADE    both,shade,0,colour4,0
  76. ;  DOUBLESIDED  double,0,0,colour5a,colour5b
  77. ;  DOUBLESHADE  double,shade,shade,colour6a,colour6b
  78. ;  1SHADE2SINE  0,shade,wavey,colour7a,colour7b
  79. ;  1SINE2SHADE  0,wavey,shade,colour8a,colour8b
  80. ;  2SINE        double,wavey,wavey,colour9a,colour9b
  81. ;  SAMPLESIDE   this is,an example,of what,you can,do!
  82. ;  HIBITMAP     himap:,0,5,5
  83. ;
  84. ; Make sure your 3DFACEs and PFACEs are entered in Counter-Clockwise order!
  85. ;
  86. ; Version 0.2:
  87. ;
  88. ;  DXF to 3DV now accepts REND386 .PLG's!!  Note: The maximum number of
  89. ;  verticies in any PLG is defined by maxpolyline (see below)
  90. ;
  91. ;  The materials file can now how the keyword RND in it to generate a random
  92. ;  number   between  0  and  255.  This  may  be  useful  for  sine  texture
  93. ;  mis-alignment.
  94. ;
  95. ; Version 0.3:
  96. ;
  97. ;  Colons can be placed in materials to output text/data/comments after a
  98. ;  polygon face definition.
  99. ;   Consider a material like so
  100. ;    MATERIALXY   0,mesh,0,colourxy,0:;hello there
  101. ;   will return a face line like so
  102. ;          dw 0,mesh,0,colourxy,0,1,2,3,6,7,1;hello there
  103. ;
  104. ;  Bitmaps can now be defined in AutoCAD and then placed as part of an object.
  105. ;  Notice how I used the new and fancy colon option?
  106. ;   eg: a material of
  107. ;    HIBITMAP     himap:,0,5,5   ;testtest
  108. ;   will return a line of
  109. ;          dw himap,29,0,5,5   ;testtest
  110. ;
  111. ;  To implement the bitmap option, define a 3dface in AutoCAD   on  the  layer
  112. ;  hibitmap.  DXF23DV will find the layer, match it up with the material,  and
  113. ;  scan for the text "map" in the  material.  If it is found, it will make the
  114. ;  surface into a bitmap.  This is good for adding  explosions/static  bitmaps
  115. ;  to dead objects.  Check out XWING to see what I mean.
  116. ;
  117. ; Version 0.4:
  118. ;
  119. ;  Connection data  is  sorted  according  to  surface  normal.   This  is  in
  120. ;  preparation for iteration handling.
  121. ;
  122. ;  Small bug with .PLG detection fixed. Also fixed a bug with the .PLG surface
  123. ;  loading.  Who uses .PLG's anyway right?  Fixed...
  124. ;
  125. ;  PFACES now work correctly
  126. ;
  127. ; Version 0.5:
  128. ;
  129. ;  The option for direct layer name to material has been added. Although this
  130. ;  kind of makes the materials file useless and if you make objects like this
  131. ;  it could be tough to edit the surface type later.  But anyway, use it like
  132. ;  so:  If the layer begins with an underscore, direct copy will take place.
  133. ;
  134. ;    _0_shade_0_16_0        <= layer name
  135. ;
  136. ;  This will directly copy the layer name into the object file replacing all _
  137. ;  characters with a commas.  Result: 0,shade,0,16,0
  138. ;
  139. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  140.  
  141. ; Link this with PMODE, FILE, and ARGC
  142.  
  143.           .386p
  144.           jumps
  145.  
  146. code32    segment para public use32
  147.           assume cs:code32, ds:code32, ss:code32
  148.  
  149.           include pmode.ext
  150.           include file.ext
  151.           include argc.ext
  152.  
  153.           public  _main
  154.  
  155. matbufsize  = 1500    ; materials list buffer size
  156. yes         = 1
  157. no          = 0
  158.  
  159. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  160. ; Macros
  161. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  162.  
  163. upper     macro regx  ; make register uppercase
  164.           local strl
  165.           cmp regx,"a"
  166.           jb short strl
  167.           cmp regx,"z"
  168.           ja short strl
  169.           sub regx,"a"-"A"
  170. strl:
  171.           endm
  172.  
  173. imul32    macro regx
  174.           imul regx
  175.           shld edx,eax,1
  176.           inc edx
  177.           shr edx,1
  178.           movsx eax,dx
  179.           endm
  180.  
  181. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  182. ; DATA
  183. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  184.  
  185. inputname       db 60 dup (?)
  186. outputname      db 60 dup (?)
  187.  
  188. buffer          db 60 dup (?)
  189.  
  190. nx              dd 0      ; translation of location
  191. ny              dd 0
  192. nz              dd 0
  193. scale           dd 0      ; overall scale done after move
  194. nu              dd 0      ; translation of location after scale
  195. nv              dd 0
  196. nw              dd 0
  197.  
  198. randomnumber    dd 5fe6c809h
  199.  
  200. fileloc         dd 0      ; current location in DXF file (offset)
  201.  
  202. dxfo            dd 0      ; start dxf file location
  203. dxfsize         dd 0      ; dxf filesize
  204. mato            dd 0      ; materials file location
  205. matsize         dd 0      ; materials file size
  206. materials       dd 0      ; materials/layer names offset
  207. facetill        dd 0      ; temp 3dface and poly gather length
  208.  
  209. maxpoints       = 3000    ; max number of unique points in any DXF
  210. maxconns        = 4000    ; maximum connections
  211. maxpolyline     = 3000    ; maximum pface verticies
  212.  
  213. ox              = 0       ; offsets for points (from conmem)
  214. oy              = ox+maxpoints*4
  215. oz              = oy+maxpoints*4
  216. sd              = oz+maxpoints*4 ; polygon number
  217. p1              = sd+maxconns*4 ; point 1
  218. p2              = p1+maxconns*4 ; point 2
  219. wg              = p2+maxconns*4 ; where going
  220. wc              = wg+maxconns*4 ; where came
  221. ea              = wc+maxconns*4 ; a,b,c,d of plane equation
  222. eb              = ea+maxconns*4
  223. ec              = eb+maxconns*4
  224. ed              = ec+maxconns*4
  225. mt              = ed+maxconns*4 ; texture/layer name
  226. sk              = mt+maxconns*4 ; temp data for sorting
  227. bv              = sk+maxpolyline*4
  228.  
  229. memoryneeded    = oy+oz+sd+p1+p2+wg+wc+ea+eb+ec+ed+mt+sk+bv+maxpolyline*4
  230.  
  231. connmem         dd 0      ; memory for points/connection data
  232. points          dd 0      ; number of points
  233. surfaces        dd 0      ; number of surfaces
  234. connections     dd 0      ; number of connections
  235. thismaterial    dd 0      ; current/working material
  236. iterate         db 0      ; flag to use iteration
  237. shouldisort     db 0      ; flag for sorting - always if iteration.
  238. layer           db 0      ; flag for selective layer processing
  239. tnormal         db 0      ; flag for true surface normal output
  240. yneg            db 0      ; flag for y negation
  241. temp1           dd 0
  242. temp2           dd 0
  243. temp3           dd 0
  244. temp4           dd 0
  245.  
  246. polyindex       dd maxpolyline dup (0)
  247. start           dd 0
  248. startconn       dd 0
  249. pvertexs        dd 0
  250. pfaces          dd 0
  251. colon           dd 0           ; position of colon 0 = none, x = position
  252.  
  253. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  254. ; CODE
  255. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  256.  
  257. include extras.rt
  258.  
  259. errmsg0 db 13,10,'Missing Filename!',0dh,0ah,"$"
  260. errmsg1 db 13,10,'Not Enough Memory!',0dh,0ah,"$"
  261. errmsg2 db 13,10,'Error Opening File!',0dh,0ah,"$"
  262. errmsg3 db 13,10,'Error Loading Materials File!',0dh,0ah,"$"
  263. errmsg4 db 13,10,'Error:Too Many Points in File!',0dh,0ah,"$"
  264. errmsg5 db 13,10,'Error:No 3DFACEs or PFACEs Found in DXF!',0dh,0ah,"$"
  265. errmsg6 db 13,10,'Dont Know How To Handle Parts Of This DXF!',0dh,0ah,"$"
  266. errmsg7 db 13,10,'Error:Nothing to do in .PLG!',0dh,0ah,"$"
  267. okmsg   db 13,10,"AutoCAD .DXF to 3DVECTORS converter by John McCarthy V0.5 (also does .PLG)"
  268.         db 13,10,"DXF23DV inputname outputname [-s# -x# -y# -z# -mfilename -u# -v# -w# -l -n -q]",10,13
  269.         db 13,10," -x  x translation for object (before scale) - can be floating point, +z = up"
  270.         db 13,10," -y  y translation for object (before scale) - can be floating point, +z = up"
  271.         db 13,10," -z  z translation for object (before scale) - can be floating point, +z = up"
  272.         db 13,10," -s  scale factor                            - can be floating point"
  273.         db 13,10," -u  x translation for object (after scale)  - integer only, +y = down"
  274.         db 13,10," -v  y translation for object (after scale)  - integer only, +y = down"
  275.         db 13,10," -w  z translation for object (after scale)  - integer only, +y = down"
  276.         db 13,10," -m  materials list filename (corresponds to layer names)"
  277.         db 13,10," -l  selective layer processing (only process layers that are found"
  278.         db 13,10,"     in materials file)"
  279.         db 13,10," -n  output true calculated surface normal (otherwise 0,0,0), useless option"
  280.         db 13,10," -q  negate Y output axis"
  281.         db 13,10," -b  sort surfaces based on surface normal (default=sort)",13,10
  282.         db 13,10,"Use 3DFACE and PFACE to define your ACAD object."
  283.         db 13,10,"Enter your 3DFACEs and PFACEs in Counter-Clockwise order."
  284.         db 13,10,"BRIAN.MCCARTHY@CANREM.COM",13,10,"$"
  285. okmsg1  db 13,10,"DXF23DV Converter V0.5 - This conversion conforms to version 3DVECT35",13,10,"$"
  286. okmsg2  db 13,10,"Materials Found (Layers):",13,10,"$"
  287. okmsg3  db 13,10,"Conversion Complete.",13,10,"  Points: $"
  288. okmsg4  db "   Surfaces: $"
  289. okmsg5  db "Writing...",13,10,"$"
  290. okmsg6  db "Sorting...",13,10,"$"
  291. okmsg7  db "Done.",13,10,"$"
  292. okmsg8  db "File type: .DXF",13,10,"$"
  293. okmsg9  db "File type: .PLG",13,10,"$"
  294.  
  295. exiterr0:
  296.         mov edx,offset errmsg0
  297.         call _putdosmsg
  298.         jmp okerr0
  299. exiterr1:
  300.         mov edx,offset errmsg1
  301.         call _putdosmsg
  302.         jmp okerr0
  303. exiterr2:
  304.         mov edx,offset errmsg2
  305.         call _putdosmsg
  306.         jmp okerr0
  307. exiterr3:
  308.         mov edx,offset errmsg3
  309.         call _putdosmsg
  310.         jmp okerr0
  311. exiterr4:
  312.         mov edx,offset errmsg4
  313.         call _putdosmsg
  314.         jmp okerr0
  315. exiterr5:
  316.         mov edx,offset errmsg5
  317.         call _putdosmsg
  318.         jmp okerr0
  319. exiterr6:
  320.         mov edx,offset errmsg6
  321.         call _putdosmsg
  322.         jmp okerr0
  323. exiterr7:
  324.         mov edx,offset errmsg7
  325.         call _putdosmsg
  326.         jmp okerr0
  327. okerr0:
  328.         mov edx,offset okmsg
  329.         call _putdosmsg
  330.         jmp _exit
  331.  
  332. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  333. ; Allocate memory
  334. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  335. _main:
  336.         call _setbuf
  337.  
  338.         mov eax,matbufsize  ; allocate memory for materials
  339.         call _getmem
  340.         jc exiterr1
  341.         mov materials,eax
  342.         mov edi,eax
  343.         mov ecx,matbufsize/4 ; wipe materials buffer
  344.         xor eax,eax
  345.         rep stosd
  346.  
  347.         mov eax,memoryneeded
  348.         call _getmem
  349.         jc exiterr1
  350.         mov connmem,eax
  351.         mov edi,eax
  352.         mov ecx,memoryneeded
  353.         xor eax,eax
  354.         rep stosb
  355.  
  356.         mov points,eax
  357.         mov surfaces,eax
  358.         mov connections,eax
  359.  
  360. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  361. ; Parse and open DXF, allocate memory, load DXF, close file
  362. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  363.  
  364.         xor al,al
  365.         mov edx,offset inputname
  366.         call _cchekstr
  367.         jc exiterr0
  368.         mov edx,offset inputname
  369.         call _openfile
  370.         jc exiterr2
  371.         call _filesize
  372.         mov dxfsize,eax
  373.         call _getmem
  374.         jc exiterr1
  375.         mov dxfo,eax
  376.         mov edx,eax
  377.         mov ecx,dxfsize
  378.         call _readfile
  379.         jc exiterr2
  380.         call _closefile
  381.         mov edx,dxfo
  382.         mov ecx,dxfsize
  383.         call replace
  384.  
  385. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  386. ; Parse and open materials file, allocate memory, load file, close file
  387. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  388.  
  389.         mov matsize,0
  390.         mov al,"m"                      ; check for filename on commandline
  391.         mov edx,offset buffer
  392.         call _ccheksstr
  393.         jc nomaters
  394.         mov edx,offset buffer
  395.         call _openfile
  396.         jc exiterr3
  397.         call _filesize
  398.         mov matsize,eax
  399.         call _getmem
  400.         jc exiterr1
  401.         mov mato,eax
  402.         mov edx,eax
  403.         mov ecx,matsize
  404.         call _readfile
  405.         jc exiterr3
  406.         call _closefile
  407.         mov edx,mato
  408.         mov ecx,matsize
  409.         call replace
  410. nomaters:
  411.  
  412. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  413. ; Parse and open output filename
  414. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  415.  
  416.         mov al,1                        ; check for filename on commandline
  417.         mov edx,offset outputname
  418.         call _cchekstr
  419.         jc exiterr0
  420.         mov edx,offset outputname
  421.         call _createfile
  422.         jc exiterr0
  423.  
  424. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  425. ; Get scaling factor for points
  426. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  427.  
  428.         mov scale,1*65536
  429.         mov al,"s"
  430.         mov edx,offset buffer
  431.         call _ccheksstr
  432.         jc noscale
  433.         mov edx,offset buffer
  434.         mov eax,dword ptr buffer
  435.         call _get_float32
  436.         mov scale,eax
  437. noscale:
  438.  
  439. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  440. ; Get position offset for points
  441. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  442.  
  443.         mov nx,0
  444.         mov al,"x"
  445.         mov edx,offset buffer
  446.         call _ccheksstr
  447.         jc nox
  448.         mov edx,offset buffer
  449.         call _get_float32
  450.         mov nx,eax
  451. nox:
  452.         mov ny,0
  453.         mov al,"y"
  454.         mov edx,offset buffer
  455.         call _ccheksstr
  456.         jc noy
  457.         mov edx,offset buffer
  458.         call _get_float32
  459.         mov ny,eax
  460. noy:
  461.         mov nz,0
  462.         mov al,"z"
  463.         mov edx,offset buffer
  464.         call _ccheksstr
  465.         jc noz
  466.         mov edx,offset buffer
  467.         call _get_float32
  468.         mov nz,eax
  469. noz:
  470.         mov nu,0
  471.         mov al,"u"
  472.         mov edx,offset buffer
  473.         call _ccheksstr
  474.         jc nou
  475.         mov edx,offset buffer
  476.         call _strhtn
  477.         call _vct32
  478.         mov nu,eax
  479. nou:
  480.         mov nv,0
  481.         mov al,"v"
  482.         mov edx,offset buffer
  483.         call _ccheksstr
  484.         jc nov
  485.         mov edx,offset buffer
  486.         call _strhtn
  487.         call _vct32
  488.         mov nv,eax
  489. nov:
  490.         mov nw,0
  491.         mov al,"w"
  492.         mov edx,offset buffer
  493.         call _ccheksstr
  494.         jc now
  495.         mov edx,offset buffer
  496.         call _strhtn
  497.         call _vct32
  498.         mov nw,eax
  499. now:
  500.  
  501. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  502. ; Hunt for -i iteration option
  503. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  504.  
  505.         mov shouldisort,yes
  506.         mov iterate,no
  507.         mov al,"i"
  508.         call _cchekswitchnc
  509.         jc yesiterate
  510.         mov iterate,yes
  511.         jmp pastsort
  512. yesiterate:
  513.  
  514. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  515. ; Hunt for -b sorting option
  516. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  517.  
  518.         mov al,"b"
  519.         call _cchekswitchnc
  520.         jc pastsort
  521.         mov shouldisort,no
  522. pastsort:
  523.  
  524. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  525. ; Hunt for -l selective layer option
  526. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  527.  
  528.         mov layer,no
  529.         mov al,"l"
  530.         call _cchekswitchnc
  531.         jc yeslayer
  532.         mov layer,yes
  533. yeslayer:
  534.  
  535. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  536. ; Hunt for -q y negation option
  537. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  538.  
  539.         mov yneg,no
  540.         mov al,"q"
  541.         call _cchekswitchnc
  542.         jc yesneg
  543.         mov yneg,yes
  544. yesneg:
  545.  
  546. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  547. ; Hunt for -n normal output
  548. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  549.  
  550.         mov tnormal,no
  551.         mov al,"n"
  552.         call _cchekswitchnc
  553.         jc yesnormal
  554.         mov tnormal,yes
  555. yesnormal:
  556.  
  557. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  558. ; Test DXF or PLG
  559. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  560.  
  561.         mov edx,offset okmsg1
  562.         call _putdosmsg
  563.         call isitadxforplg
  564.         jc do_plg
  565.  
  566. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  567. ; Split DXF into single connection data, calc normals, assign materials
  568. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  569.  
  570.         mov edx,offset okmsg8
  571.         call _putdosmsg
  572.         mov edx,offset okmsg2
  573.         call _putdosmsg
  574.  
  575.         mov ebp,dxfsize
  576.         add ebp,dxfo
  577.         mov facetill,ebp
  578.  
  579.         mov eax,dxfo
  580.         mov fileloc,eax
  581.         call dxf_3dfaces
  582.  
  583.         mov eax,dxfo
  584.         mov fileloc,eax
  585.         call dxf_polylines
  586.  
  587.         cmp surfaces,0
  588.         jz exiterr5
  589.         jmp cont
  590.  
  591. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  592. ; Input PLG
  593. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  594.  
  595. do_plg:
  596.         mov edx,offset okmsg9
  597.         call _putdosmsg
  598.         mov edx,offset okmsg2
  599.         call _putdosmsg
  600.  
  601.         call plg_getall
  602.         cmp surfaces,0
  603.         jz exiterr7
  604.  
  605. cont:
  606.         mov edx,offset okmsg3
  607.         call _putdosmsg
  608.         mov eax,points
  609.         call _dos_dec16
  610.         mov edx,offset okmsg4
  611.         call _putdosmsg
  612.         mov eax,surfaces
  613.         call _dos_dec16
  614.         call _dos_ret
  615.         call _dos_ret
  616.  
  617.         call renumber_surfaces
  618.         call collect_points
  619.         call wipe_d
  620.         call sort_them
  621.         mov edx,offset okmsg5
  622.         call _putdosmsg
  623.         call output_file
  624.         mov edx,offset okmsg7
  625.         call _putdosmsg
  626.         call _closefile
  627.         jmp _exit
  628.  
  629. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  630. ; Search for and (if neccessary) build materials list
  631. ;  In:
  632. ;   EDX => materials string (layer name) eg db "OBJECT1 "
  633. ;  Out:
  634. ;   EAX = assigned material number
  635. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  636.  
  637. assign_material:
  638.         mov esi,materials
  639.         mov edi,edx
  640.         xor ebp,ebp
  641. amloopb:
  642.         cmp byte ptr [esi],0 ; test if at end of list => add on to list
  643.         je amadditz
  644. amloop:
  645.         mov al,[esi]         ; check if material is already in list
  646.         mov bl,[edi]
  647.         inc esi
  648.         inc edi
  649.         mov cl,al
  650.         or cl,bl
  651.         jz amout
  652.  
  653.         cmp al,bl
  654.         je amloop
  655. amout:
  656.         dec esi
  657.         cmp al,0
  658.         jne amabort
  659.  
  660.         cmp bl,"0"
  661.         jae amabortx
  662.  
  663.         mov eax,ebp          ; material already present, return number
  664.         ret
  665. amadditz:
  666.         push esi
  667. amaddit:
  668.         mov al,[edx]         ; material not found at all, add to list
  669.         mov [esi],al
  670.         inc edx
  671.         inc esi
  672.         cmp al,"0"
  673.         jae amaddit
  674.  
  675.         pop edx
  676.         push ebp
  677.         push esi
  678.         mov al,"$"
  679.         mov [esi-1],al
  680.         call _putdosmsg
  681.         call _dos_ret
  682.         pop esi
  683.  
  684.         xor al,al
  685.         mov [esi-1],al
  686.         pop eax
  687.  
  688.         ret
  689. amabort:
  690.         inc esi              ; material not found, continue checking
  691.         cmp byte ptr [esi],0
  692.         jne amabort
  693.  
  694. amabortx:
  695.         inc esi
  696.         inc ebp
  697.         mov edi,edx
  698.         jmp amloopb
  699.  
  700. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  701. ; Find material number ECX
  702. ; In:  ECX = material number to find
  703. ; Out: EDX => location of material name (name only, use this to find material in file)
  704. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  705. find_ecx:
  706.         mov edx,materials
  707.         cmp ecx,0
  708.         je _ret
  709. mcxb:
  710.         inc edx
  711.         cmp byte ptr [edx],0
  712.         jne mcxb
  713.         loop mcxb
  714.  
  715.         inc edx
  716.         ret
  717.  
  718. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  719. ; Unpad string
  720. ; In:  EDX => string eg " , Hello th"
  721. ; Out: EDX => string (after spaces, colons, whatever) eg "Hello th"
  722. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  723. unpad:
  724.         dec edx
  725. upx:
  726.         inc edx
  727.         mov al,[edx]
  728.         cmp al,"-"
  729.         je upretx
  730.         cmp al,"."
  731.         je upretx
  732.         cmp al,"#"
  733.         je upretx
  734.         cmp al,"A"
  735.         jae upretx
  736.         cmp al,"9"
  737.         ja upx
  738.         cmp al,"0"
  739.         jb upx
  740. upretx:
  741.         ret
  742.  
  743.         db "**Hey - What are you doing ripping my code?!?!**"
  744.  
  745. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  746. ; Next string
  747. ; In:  EDX => string eg "Hello there mi"
  748. ; Out: EDX => next string (after spaces, colons, whatever) eg "there mi"
  749. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  750. next:
  751.         call unpad
  752.         dec edx
  753. nxc:
  754.         inc edx
  755.         mov al,[edx]
  756.         cmp al,"-"
  757.         je nxc
  758.         cmp al,"."
  759.         je nxc
  760.         cmp al,"#"
  761.         je nxc
  762.         cmp al,"A"
  763.         jae nxc
  764.         cmp al,"9"
  765.         ja nxretc
  766.         cmp al,"0"
  767.         jae nxc
  768. nxretc:
  769.         jmp unpad
  770.  
  771. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  772. ; Search_string: Find string at EDX in DXF file
  773. ; In:
  774. ;   EDX => ASCIIZ string to search for (DXF)
  775. ;   EDI = location to start search
  776. ;   EBP = location to end search
  777. ; Out:
  778. ;  CF = 1 - not found
  779. ;  CF = 0 - found
  780. ;   EDI = location where found
  781. ; Notes: String at EDI must have a space or zero at end for search tp succeed.
  782. ;  eg:  EDX => "HELLO",0
  783. ;       EDI => "ABCDHELLOEFGI" will FAIL! - but " ABCDHELLO DKJ" will succeed!
  784. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  785.  
  786. search_string:
  787.         mov esi,edx
  788.         mov ecx,edi
  789. ssloop:
  790.         mov al,[esi]
  791.         mov ah,[ecx]
  792.         upper al
  793.         upper ah
  794.         inc esi
  795.         inc ecx
  796.         mov bl,al
  797.         or bl,ah
  798.         jz ssout
  799.  
  800.         cmp al,ah
  801.         je ssloop
  802.  
  803.         cmp al,0
  804.         jne ssabort
  805.  
  806.         cmp ah,"0"
  807.         jae ssabort
  808. ssout:
  809.         clc
  810.         ret
  811. ssabort:
  812.         cmp ecx,ebp
  813.         jae ssretx
  814.  
  815.         inc edi
  816.         jmp search_string
  817. ssretx:
  818.         stc
  819.         ret
  820.  
  821. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  822. ; Search materials file for information on material ECX
  823. ; In:  ECX = material number to look for
  824. ; Out: EDX => string containing surface information (terminated with db 0,0)
  825. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  826.  
  827. find_info:
  828.         cmp matsize,0
  829.         je fidodefault
  830.  
  831.         call find_ecx
  832.         cmp byte ptr [edx],"_" ; check for directo copy of layer name to material
  833.         je _ret                ; yes, direct, exit with EDX => layer name
  834.         mov esi,edx
  835.         mov edx,mato
  836. filoop:
  837.         call unpad
  838.         xor ebx,ebx
  839. ficx:
  840.         mov al,[esi+ebx]
  841.         mov ah,[edx]
  842.         upper al
  843.         upper ah
  844.         inc ebx
  845.         inc edx
  846.         cmp al,ah
  847.         je ficx
  848.  
  849.         cmp al,0
  850.         je fifoundm
  851.  
  852.         dec edx
  853. fifinddol:
  854.         inc edx
  855.         cmp byte ptr [edx],0
  856.         jne fifinddol
  857.  
  858.         mov eax,mato
  859.         add eax,matsize
  860.         cmp edx,eax
  861.         jb filoop
  862. fidodefault:
  863.         mov edx,offset fidefault
  864.         ret
  865.  
  866. fifoundm:
  867.         dec edx
  868.         mov al,[edx]
  869.         cmp al,"A"
  870.         ja fifinddol
  871.         cmp al,"9"
  872.         ja unpad
  873.         cmp al,"0"
  874.         jae fifinddol
  875.         jmp unpad
  876.  
  877. fidefault db "0,shade,0,colour,0",0   ; for regular faces
  878. fidefaull db "0,0,0,colour,0",0       ; for lines
  879.  
  880. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  881. ; Search line for "shade" or "glow"
  882. ; In: EDX => string containing surface information (terminated with db 0,0)
  883. ; Out: CF = 0 shade found, CF = 1 shade not found    EDX = ?
  884. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  885. find_shade:
  886.         push edx
  887. find_shadeq:
  888.         mov al,[edx]
  889.         cmp al,0
  890.         je fsnoshade
  891.         inc edx
  892.  
  893.         upper al
  894.         cmp al,"S"
  895.         jne find_shadeq
  896.         mov al,[edx+0]
  897.         upper al
  898.         cmp al,"H"
  899.         jne find_shadeq
  900.         mov al,[edx+1]
  901.         upper al
  902.         cmp al,"A"
  903.         jne find_shadeq
  904.         mov al,[edx+2]
  905.         upper al
  906.         cmp al,"D"
  907.         jne find_shadeq
  908.         mov al,[edx+3]
  909.         upper al
  910.         cmp al,"E"
  911.         jne find_shadeq
  912.  
  913.         pop edx
  914.         clc
  915.         ret
  916.  
  917. fsnoshade:
  918.         pop edx
  919. find_shadez:
  920.         mov al,[edx]
  921.         cmp al,0
  922.         je fsnoshadez
  923.         inc edx
  924.  
  925.         upper al
  926.         cmp al,"G"
  927.         jne find_shadez
  928.         mov al,[edx+0]
  929.         upper al
  930.         cmp al,"L"
  931.         jne find_shadez
  932.         mov al,[edx+1]
  933.         upper al
  934.         cmp al,"O"
  935.         jne find_shadez
  936.         mov al,[edx+2]
  937.         upper al
  938.         cmp al,"W"
  939.         jne find_shadez
  940.  
  941.         clc
  942.         ret
  943. fsnoshadez:
  944.         stc
  945.         ret
  946.  
  947. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  948. ; Search line for "map"
  949. ; In: EDX => string containing surface information (terminated with db 0,0)
  950. ; Out: CF = 0 map found, CF = 1 map not found    EDX = ?
  951. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  952. find_map:
  953.         push edx
  954. find_mapq:
  955.         mov al,[edx]
  956.         cmp al,0
  957.         je fs_nomap
  958.         inc edx
  959.  
  960.         upper al
  961.         cmp al,"M"
  962.         jne find_mapq
  963.         mov al,[edx+0]
  964.         upper al
  965.         cmp al,"A"
  966.         jne find_mapq
  967.         mov al,[edx+1]
  968.         upper al
  969.         cmp al,"P"
  970.         jne find_mapq
  971.  
  972.         pop edx
  973.         clc
  974.         ret
  975.  
  976. fs_nomap:
  977.         pop edx
  978.         stc
  979.         ret
  980. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  981. ; Replace 13,10 with 0,0
  982. ; In: EDX => start location
  983. ;     ECX = length
  984. ; Out: ?
  985. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  986. replace:
  987.         inc ecx
  988.         push ecx
  989.         mov al,10
  990.         mov edi,edx
  991. re10:
  992.         repnz scasb
  993.         cmp ecx,0
  994.         je re13
  995.         mov byte ptr [edi-1],0
  996.         jmp re10
  997. re13:
  998.         mov al,13
  999.         mov edi,edx
  1000.         pop ecx
  1001. re13s:
  1002.         repnz scasb
  1003.         cmp ecx,0
  1004.         je _ret
  1005.         mov byte ptr [edi-1],0
  1006.         jmp re13s
  1007.  
  1008. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1009. ; Replace " " with 0
  1010. ; In: EDX => start location
  1011. ;     ECX = length
  1012. ; Out: ?
  1013. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1014. replacesp:
  1015.         inc ecx
  1016.         push ecx
  1017.         mov al," "
  1018.         mov edi,edx
  1019. resp:
  1020.         repnz scasb
  1021.         cmp ecx,0
  1022.         je _ret
  1023.         mov byte ptr [edi-1],0
  1024.         jmp resp
  1025.  
  1026. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1027. ; Auto calculate normal
  1028. ; In:
  1029. ;  EBX = point 1
  1030. ;  ECX = point 2
  1031. ;  EBP = point 3
  1032. ; Out:
  1033. ;    EBX = finx = x of surface normal of triangle
  1034. ;    ECX = finy = y of surface normal of triangle
  1035. ;    EBP = finz = z of surface normal of triangle
  1036. ;    EAX = D of equation (Ax+By+Cz=D)
  1037. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1038. acalc_normal:
  1039.         mov esi,connmem
  1040.  
  1041.         mov eax,[esi+ebx*4+ox]
  1042.         mov edx,[esi+ebx*4+oy]
  1043.         mov edi,[esi+ebx*4+oz]
  1044.         mov lx1,eax
  1045.         mov ly1,edx
  1046.         mov lz1,edi
  1047.  
  1048.         mov eax,[esi+ecx*4+ox]
  1049.         mov edx,[esi+ecx*4+oy]
  1050.         mov edi,[esi+ecx*4+oz]
  1051.         mov lx2,eax
  1052.         mov ly2,edx
  1053.         mov lz2,edi
  1054.  
  1055.         mov eax,[esi+ebp*4+ox]
  1056.         mov edx,[esi+ebp*4+oy]
  1057.         mov edi,[esi+ebp*4+oz]
  1058.         mov lx3,eax
  1059.         mov ly3,edx
  1060.         mov lz3,edi
  1061.  
  1062.         call calc_normal
  1063.         call calc_d
  1064.         ret
  1065.  
  1066. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1067. ;
  1068. ; Calc_normal: calculate surface normal
  1069. ;
  1070. ; In:
  1071. ;    LX1 - x of point 1 on triangle
  1072. ;    LY1 - y of point 1 on triangle
  1073. ;    LZ1 - z of point 1 on triangle
  1074. ;    LX2 - x of point 2 on triangle
  1075. ;    LY2 - y of point 2 on triangle
  1076. ;    LZ2 - z of point 2 on triangle
  1077. ;    LX3 - x of point 3 on triangle
  1078. ;    LY3 - y of point 3 on triangle
  1079. ;    LZ3 - z of point 3 on triangle
  1080. ;
  1081. ; Out:
  1082. ;    EBX = finx = x of surface normal of triangle
  1083. ;    ECX = finy = y of surface normal of triangle
  1084. ;    EBP = finz = z of surface normal of triangle
  1085. ;
  1086. ; Notes:
  1087. ; x2 = x2 - x1
  1088. ; y2 = y2 - y1
  1089. ; z2 = z2 - z1
  1090. ;
  1091. ; x3 = x3 - x1
  1092. ; y3 = y3 - y1
  1093. ; z3 = z3 - z1
  1094. ;
  1095. ; x = y2 * z3 - z2 * y3
  1096. ; y = z2 * x3 - x2 * z3
  1097. ; z = x2 * y3 - y2 * x3
  1098. ;
  1099. ; a = SQR(x ^ 2 + y ^ 2 + z ^ 2)
  1100. ;
  1101. ; x = INT(x / a * 256 + .5)
  1102. ; y = INT(y / a * 256 + .5)
  1103. ; z = INT(z / a * 256 + .5)
  1104. ;
  1105. ; This worked for me on the first try!
  1106. ;
  1107. ; If you wanted to get the equation of a plane, you could do this after:
  1108. ;  d = - x * x1 - y * y1 - z * z1
  1109. ;
  1110. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1111.  
  1112. lx1  dd 0
  1113. ly1  dd 0
  1114. lz1  dd 0
  1115.  
  1116. lx2  dd 0
  1117. ly2  dd 0
  1118. lz2  dd 0
  1119.  
  1120. lx3  dd 0
  1121. ly3  dd 0
  1122. lz3  dd 0
  1123.  
  1124. finx dd 0
  1125. finy dd 0
  1126. finz dd 0
  1127.  
  1128. calc_normal:
  1129.          mov ebx,lx1
  1130.          mov ecx,ly1
  1131.          mov ebp,lz1
  1132.  
  1133.          sub lx2,ebx
  1134.          sub ly2,ecx
  1135.          sub lz2,ebp
  1136.  
  1137.          sub lx3,ebx
  1138.          sub ly3,ecx
  1139.          sub lz3,ebp
  1140.  
  1141.          mov eax,ly2
  1142.          mov ebx,lz3
  1143.          imul ebx
  1144.          mov ecx,eax
  1145.  
  1146.          mov eax,lz2
  1147.          mov ebx,ly3
  1148.          imul ebx
  1149.          sub ecx,eax
  1150.  
  1151.          mov finx,ecx ; save x of normal
  1152.  
  1153.          mov eax,lz2
  1154.          mov ebx,lx3
  1155.          imul ebx
  1156.          mov ecx,eax
  1157.  
  1158.          mov eax,lx2
  1159.          mov ebx,lz3
  1160.          imul ebx
  1161.          sub ecx,eax
  1162.  
  1163.          mov finy,ecx ; save y of normal
  1164.  
  1165.          mov eax,lx2
  1166.          mov ebx,ly3
  1167.          imul ebx
  1168.          mov ecx,eax
  1169.  
  1170.          mov eax,ly2
  1171.          mov ebx,lx3
  1172.          imul ebx
  1173.          sub ecx,eax
  1174.  
  1175.          mov finz,ecx ; save z of normal
  1176.  
  1177. calc_testloop:
  1178.          cmp finx,32768 ; make sure (normal^2)*2 is < 2^32
  1179.          jge calc_shrit
  1180.          cmp finy,32768
  1181.          jge calc_shrit
  1182.          cmp finz,32768
  1183.          jge calc_shrit
  1184.  
  1185.          cmp finx,-32768
  1186.          jle calc_shrit
  1187.          cmp finy,-32768
  1188.          jle calc_shrit
  1189.          cmp finz,-32768
  1190.          jg  ok_2_bite_dust
  1191.  
  1192. calc_shrit:
  1193.          shr finx,1   ; calculations will be too large if squared, div by 2
  1194.          test finx,40000000h
  1195.          jz no_neg_calc1
  1196.          or   finx,80000000h
  1197. no_neg_calc1:
  1198.          shr finy,1
  1199.          test finy,40000000h
  1200.          jz no_neg_calc2
  1201.          or   finy,80000000h
  1202. no_neg_calc2:
  1203.          shr finz,1
  1204.          test finz,40000000h
  1205.          jz no_neg_calc3
  1206.          or   finz,80000000h
  1207. no_neg_calc3:
  1208.          jmp calc_testloop
  1209.  
  1210. ok_2_bite_dust:
  1211.          mov eax,finx ; x^2
  1212.          mov edi,eax  ; objects
  1213.          imul edi
  1214.          mov edi,eax
  1215.  
  1216.          mov eax,finy ; y^2
  1217.          mov esi,eax
  1218.          imul esi
  1219.          mov esi,eax
  1220.  
  1221.          mov eax,finz ; z^2
  1222.          mov ebp,eax
  1223.          imul ebp
  1224.  
  1225.          add eax,esi
  1226.          add eax,edi
  1227.  
  1228.          call sqrt    ; get square root of number
  1229.  
  1230.          mov ecx,eax
  1231.          cmp ecx,0
  1232.          je lam_abort ; should never happen!
  1233.  
  1234.          mov eax,finx
  1235.          cdq
  1236.          shld edx,eax,10
  1237.          shl eax,10   ; set unit vector to 2^12
  1238.          idiv ecx
  1239.          mov finx,eax
  1240.  
  1241.          mov eax,finy
  1242.          cdq
  1243.          shld edx,eax,10
  1244.          shl eax,10
  1245.          idiv ecx
  1246.          mov finy,eax
  1247.  
  1248.          mov eax,finz
  1249.          cdq
  1250.          shld edx,eax,10
  1251.          shl eax,10
  1252.          idiv ecx
  1253.          mov finz,eax
  1254.  
  1255.          mov ebx,finx
  1256.          mov ecx,finy
  1257.          mov ebp,finz
  1258.  
  1259. lam_abort:
  1260.          ret
  1261.  
  1262. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1263. ; Calc_D: Calculate D portion of equation of a plane
  1264. ; In:
  1265. ;    EBX = x of surface normal of triangle
  1266. ;    ECX = y of surface normal of triangle
  1267. ;    EBP = z of surface normal of triangle
  1268. ;    LX1 - x of point on triangle (any point)
  1269. ;    LY1 - y of point on triangle
  1270. ;    LZ1 - z of point on triangle
  1271. ; Out:
  1272. ;    EDX = EAX = D of equation (Ax+By+Cz=D)
  1273. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1274. calc_d:
  1275.          mov eax,lx1
  1276.          imul ebx
  1277.          mov esi,eax
  1278.  
  1279.          mov eax,ly1
  1280.          imul ecx
  1281.          add esi,eax
  1282.  
  1283.          mov eax,lz1
  1284.          imul ebp
  1285.          add eax,esi
  1286.  
  1287.          shr eax,2          ; reduce result to avoid inaccuracy
  1288.          test eax,20000000h
  1289.          jz calc_o
  1290.          or  eax,0c0000000h
  1291. calc_o:
  1292.          mov edx,eax
  1293.          ret
  1294.  
  1295. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1296. ;
  1297. ; Sqrt: Routine courtesy TRAN
  1298. ;
  1299. ; In:
  1300. ;   EAX - number to take root of
  1301. ; Out:
  1302. ;   EAX - root
  1303. ;
  1304. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1305. sqrtbasetbl db 0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225
  1306. sqrt:
  1307.          pushad
  1308.          mov ebp,eax
  1309.          bsr ebx,eax
  1310.          jnz short sqrtf0
  1311.          xor ebx,ebx
  1312. sqrtf0:
  1313.          shr ebx,3
  1314.          lea eax,[ebx*8]
  1315.          mov cl,32
  1316.          sub cl,al
  1317.          rol ebp,cl
  1318.          mov eax,ebp
  1319.          movzx eax,al
  1320.          mov edi,offset sqrtbasetbl
  1321.          mov ecx,10h
  1322. sqrtl0:
  1323.          scasb
  1324.          je short sqrtl0d
  1325.          jb short sqrtl0d2
  1326.          loop sqrtl0
  1327.          inc edi
  1328. sqrtl0d2:
  1329.          dec edi
  1330.          inc cl
  1331. sqrtl0d:
  1332.          movzx edx,byte ptr [edi-1]
  1333.          dec cl
  1334.          xor cl,0fh
  1335.          mov edi,ecx
  1336.          mov ecx,ebx
  1337.          jecxz short sqrtdone
  1338.          sub eax,edx
  1339. sqrtml:
  1340.          shld eax,ebp,8
  1341.          rol ebp,8
  1342.          mov ebx,edi
  1343.          shl ebx,5
  1344.          xor edx,edx
  1345.          mov esi,eax
  1346.          div ebx
  1347.          rol edi,4
  1348.          add edi,eax
  1349.          add ebx,eax
  1350. sqrtf2:
  1351.          imul eax,ebx
  1352.          mov edx,eax
  1353.          mov eax,esi
  1354.          sub eax,edx
  1355.          jc short sqrtf1
  1356.          loop sqrtml
  1357. sqrtdone:
  1358.          mov [esp+28],edi
  1359.          popad
  1360.          ret
  1361. sqrtf1:
  1362.          dec ebx
  1363.          dec edi
  1364.          movzx eax,bl
  1365.          and al,1fh
  1366.          jmp sqrtf2
  1367.  
  1368. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1369. ; Search and Build points table
  1370. ; In:
  1371. ;    EBX - x of point
  1372. ;    ECX - y of point
  1373. ;    EBP - z of point
  1374. ; Out:
  1375. ;    EAX - assigned point number
  1376. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1377. check_point:
  1378.         mov eax,ebx
  1379.         or eax,ecx
  1380.         or eax,ebp
  1381.         jnz cpnotnull
  1382.         ret
  1383. cpnotnull:
  1384.         mov esi,connmem
  1385.         mov eax,1
  1386. cploop:
  1387.         cmp dword ptr [esi+eax*4+ox],0
  1388.         jne ckdotest
  1389.         cmp dword ptr [esi+eax*4+oy],0
  1390.         jne ckdotest
  1391.         cmp dword ptr [esi+eax*4+oz],0
  1392.         jne ckdotest
  1393.  
  1394.         mov [esi+eax*4+ox],ebx
  1395.         mov [esi+eax*4+oy],ecx
  1396.         mov [esi+eax*4+oz],ebp
  1397.  
  1398.         inc points
  1399.         cmp eax,maxpoints
  1400.         jae exiterr4
  1401.  
  1402.         ret
  1403. ckdotest:
  1404.         cmp [esi+eax*4+ox],ebx
  1405.         jne cknotest
  1406.         cmp [esi+eax*4+oy],ecx
  1407.         jne cknotest
  1408.         cmp [esi+eax*4+oz],ebp
  1409.         jne cknotest
  1410.         ret
  1411. cknotest:
  1412.         inc eax
  1413.         cmp eax,maxpoints
  1414.         jae exiterr4
  1415.  
  1416.         jmp cploop
  1417.  
  1418. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1419. ; Get next number (float) after string EDX
  1420. ; In: EDX => string to search for
  1421. ; Out:EAX = 32bit float number (fake float, you know what I mean)
  1422. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1423. getnumber:
  1424.         mov edi,fileloc
  1425.         mov ebp,dxfo
  1426.         add ebp,dxfsize
  1427.         call search_string
  1428.         jc gn_nf
  1429.         mov edx,edi
  1430.         call next
  1431.         mov fileloc,edx
  1432.         call _get_float32
  1433.         push eax
  1434.         mov edx,fileloc
  1435.         call next
  1436.         mov fileloc,edx
  1437.         pop eax
  1438.         clc
  1439. gn_nf:
  1440.         ret
  1441.  
  1442. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1443. ; Add DXF into connections, points, calculate normals and load materials.
  1444. ;
  1445. ; In:
  1446. ;   fileloc => location to start checking for 3dfaces
  1447. ;   filetill => location to end checking for 3dfaces
  1448. ;
  1449. ; This adds a DXF file to the already loaded connections (usually empty), but
  1450. ; you could mix more than one DXF file if you  really  wanted  to.   This  is
  1451. ; useful for adding only 3dfaces that are within blocks (if option set).
  1452. ;
  1453. ; Psuedo code:
  1454. ;
  1455. ;Variables p= x(p) y(p) z(p)
  1456. ;          c= sd(c) p1(c) p2(c) wg(c) wc(c) a(c) b(c) c(c) d(c) mt(c)
  1457. ; c = connections
  1458. ; p = points
  1459. ; sd = surface number
  1460. ; p1 = point 1 (start of line)
  1461. ; p2 = point 2 (end of line)
  1462. ; wg = where are we going
  1463. ; wc = where did we come from
  1464. ; a,b,c,d = equation of plane
  1465. ; mt = material texture for surface (layer name)
  1466. ;
  1467. ;search "3DFACE" not found => exit
  1468. ; search "8"
  1469. ; call next
  1470. ; assign material
  1471. ;
  1472. ;search "10" => x(p)
  1473. ;search "20" => y(p)
  1474. ;search "30" => z(p)
  1475. ;search "11" => x(p+1)
  1476. ;search "21" => y(p+1)
  1477. ;search "31" => z(p+1)
  1478. ;search "12" => x(p+2)
  1479. ;search "22" => y(p+2)
  1480. ;search "32" => z(p+2)
  1481. ;search "13" => x(p+3)
  1482. ;search "23" => y(p+3)
  1483. ;search "33" => z(p+3)
  1484. ;
  1485. ;    m(c+0)  = material
  1486. ;    p1(c+0) =(p+0)
  1487. ;    p2(c+0) =(p+1)
  1488. ;    wg(c+0) =(c+1)
  1489. ;    wc(c+0) =(c+3)
  1490. ;    m(c+1)  = material
  1491. ;    p1(c+1) =(p+0)
  1492. ;    p2(c+1) =(p+1)
  1493. ;    wg(c+1) =(c+2)
  1494. ;    wc(c+1) =(c+0)
  1495. ;    m(c+2)  = material
  1496. ;    p1(c+2) =(p+0)
  1497. ;    p2(c+2) =(p+1)
  1498. ;    wg(c+2) =(c+3)
  1499. ;    wc(c+2) =(c+1)
  1500. ;    m(c+3)  = material
  1501. ;    p1(c+3) =(p+0)
  1502. ;    p2(c+3) =(p+1)
  1503. ;    wg(c+3) =(c+0)
  1504. ;    wc(c+3) =(c+2)
  1505. ;
  1506. ;  calc normal
  1507. ;  calc D
  1508. ;  d(c+0)=D
  1509. ;  d(c+1)=D
  1510. ;  d(c+2)=D
  1511. ;  d(c+3)=D
  1512. ;
  1513. ;  if 10=40
  1514. ;  if 11=41
  1515. ;  if 12=42
  1516. ;     wg(c+2)=c+0
  1517. ;     wc(c+0)=c+2
  1518. ;     c=c-1
  1519. ;     p=p-1
  1520. ;  p=p+4
  1521. ;  c=c+4
  1522. ; goto search face
  1523. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1524.  
  1525. tag1     db "3DFACE",0
  1526. tag2     db "8",0
  1527. tagx     db "10",0,0
  1528.          db "11",0,0
  1529.          db "12",0,0
  1530.          db "13",0,0
  1531. tagy     db "20",0,0
  1532.          db "21",0,0
  1533.          db "22",0,0
  1534.          db "23",0,0
  1535. tagz     db "30",0,0
  1536.          db "31",0,0
  1537.          db "32",0,0
  1538.          db "33",0,0
  1539.  
  1540. dxf_3dfaces:
  1541.          mov edx,offset tag1
  1542.          mov edi,fileloc
  1543.          mov ebp,facetill
  1544.          call search_string
  1545.          jc _ret
  1546.  
  1547.          mov edx,offset tag2
  1548.          mov ebp,facetill
  1549.          call search_string
  1550.          jc dxf_3dfaces       ; no layer to surface?
  1551.  
  1552.          mov edx,edi
  1553.          call next
  1554.          mov fileloc,edx
  1555.          call assign_material
  1556.          mov thismaterial,eax
  1557.  
  1558.          cmp layer,no
  1559.          je dxf_nosel
  1560.          mov ecx,eax
  1561.          call find_info
  1562.          cmp edx,offset fidefault
  1563.          je dxf_3dfaces
  1564.  
  1565. dxf_nosel:
  1566.          mov temp1,0
  1567. dxf_lp1:
  1568.          mov ecx,temp1
  1569.          lea edx,[tagx+ecx*4]
  1570.          call getnumber
  1571.          add eax,nx
  1572.          mov ebx,scale
  1573.          imul32 ebx
  1574.          add eax,nu
  1575.          push eax
  1576.  
  1577.          mov ecx,temp1
  1578.          lea edx,[tagy+ecx*4]
  1579.          call getnumber
  1580.          add eax,ny
  1581.          mov ebx,scale
  1582.          imul32 ebx
  1583.          add eax,nw
  1584.          push eax
  1585.  
  1586.          mov ecx,temp1
  1587.          lea edx,[tagz+ecx*4]
  1588.          call getnumber
  1589.          add eax,nz
  1590.          mov ebx,scale
  1591.          imul32 ebx
  1592.          add eax,nv
  1593.          cmp yneg,yes
  1594.          je noyneg
  1595.          neg eax
  1596. noyneg:
  1597.          mov ecx,eax
  1598.          pop ebp
  1599.          pop ebx
  1600.          call check_point
  1601.          push eax
  1602.  
  1603.          inc temp1
  1604.          cmp temp1,4
  1605.          jne dxf_lp1
  1606.  
  1607.          pop edx ; 3
  1608.          pop ecx ; 2
  1609.          pop ebx ; 1
  1610.          pop eax ; 0
  1611.  
  1612.          mov ebp,connections
  1613.          mov esi,connmem
  1614.          mov edi,surfaces
  1615.  
  1616.          cmp eax,ecx    ; check for point: 1,1,1,1
  1617.          je dxf_point
  1618.  
  1619.          cmp eax,ecx    ; check for line: 1,2,1,2  or 1,2,1,1
  1620.          je dxf_line
  1621.  
  1622.          cmp eax,edx    ; check for triangle:  0 1 2 0 or 0 1 2 2
  1623.          je dxf_tri
  1624.          cmp ecx,edx
  1625.          jne dxf_4point ; must be a 4 point surface...
  1626. dxf_tri:
  1627.          mov [esi+(ebp+0)*4+p1],eax  ; p1(ebp)=eax
  1628.          mov [esi+(ebp+0)*4+p2],ebx  ; p2(ebp)=ebx
  1629.          mov [esi+(ebp+1)*4+p1],ebx  ; p1(ebp+1)=ebx
  1630.          mov [esi+(ebp+1)*4+p2],ecx  ; p2(ebp+1)=ecx
  1631.          mov [esi+(ebp+2)*4+p1],ecx  ; p1(ebp+2)=ecx
  1632.          mov [esi+(ebp+2)*4+p2],eax  ; p2(ebp+2)=eax
  1633.  
  1634.          mov [esi+(ebp+0)*4+wc],ebp  ; wc(ebp)=ebp+2
  1635.          mov [esi+(ebp+1)*4+wc],ebp  ; wc(ebp+1)=ebp+0
  1636.          mov [esi+(ebp+2)*4+wc],ebp  ; wc(ebp+2)=ebp+1
  1637.          add dword ptr [esi+(ebp+0)*4+wc],2
  1638.          add dword ptr [esi+(ebp+2)*4+wc],1
  1639.  
  1640.          mov [esi+(ebp+0)*4+wg],ebp  ; wg(ebp)=ebp+1
  1641.          mov [esi+(ebp+1)*4+wg],ebp  ; wg(ebp+1)=ebp+2
  1642.          mov [esi+(ebp+2)*4+wg],ebp  ; wg(ebp+2)=ebp+0
  1643.          add dword ptr [esi+(ebp+0)*4+wg],1
  1644.          add dword ptr [esi+(ebp+1)*4+wg],2
  1645.  
  1646.          mov [esi+(ebp+0)*4+sd],edi  ; sd(ebp)=surface number
  1647.          mov [esi+(ebp+1)*4+sd],edi
  1648.          mov [esi+(ebp+2)*4+sd],edi
  1649.  
  1650.          mov edi,thismaterial
  1651.          mov [esi+(ebp+0)*4+mt],edi  ; set material for surface
  1652.          mov [esi+(ebp+1)*4+mt],edi
  1653.          mov [esi+(ebp+2)*4+mt],edi
  1654.  
  1655.          mov ebp,ecx
  1656.          mov ecx,ebx
  1657.          mov ebx,eax
  1658.          call acalc_normal
  1659.  
  1660.          mov edi,connections
  1661.          mov esi,connmem
  1662.  
  1663.          mov [esi+(edi+0)*4+ea],ebx
  1664.          mov [esi+(edi+0)*4+eb],ecx
  1665.          mov [esi+(edi+0)*4+ec],ebp
  1666.          mov [esi+(edi+0)*4+ed],edx
  1667.          mov [esi+(edi+1)*4+ea],ebx
  1668.          mov [esi+(edi+1)*4+eb],ecx
  1669.          mov [esi+(edi+1)*4+ec],ebp
  1670.          mov [esi+(edi+1)*4+ed],edx
  1671.          mov [esi+(edi+2)*4+ea],ebx
  1672.          mov [esi+(edi+2)*4+eb],ecx
  1673.          mov [esi+(edi+2)*4+ec],ebp
  1674.          mov [esi+(edi+2)*4+ed],edx
  1675.  
  1676.          add surfaces,1
  1677.          add connections,3
  1678.  
  1679.          jmp dxf_3dfaces
  1680.  
  1681. dxf_4point:
  1682.          mov [esi+(ebp+0)*4+p1],eax  ; p1(ebp)=eax
  1683.          mov [esi+(ebp+0)*4+p2],ebx  ; p2(ebp)=ebx
  1684.          mov [esi+(ebp+1)*4+p1],ebx  ; p1(ebp+1)=ebx
  1685.          mov [esi+(ebp+1)*4+p2],ecx  ; p2(ebp+1)=ecx
  1686.          mov [esi+(ebp+2)*4+p1],ecx  ; p1(ebp+2)=ecx
  1687.          mov [esi+(ebp+2)*4+p2],edx  ; p2(ebp+2)=edx
  1688.          mov [esi+(ebp+3)*4+p1],edx  ; p1(ebp+3)=edx
  1689.          mov [esi+(ebp+3)*4+p2],eax  ; p2(ebp+3)=eax
  1690.  
  1691.          mov [esi+(ebp+0)*4+wc],ebp  ; wc(ebp)=ebp+3
  1692.          mov [esi+(ebp+1)*4+wc],ebp  ; wc(ebp+1)=ebp+0
  1693.          mov [esi+(ebp+2)*4+wc],ebp  ; wc(ebp+2)=ebp+1
  1694.          mov [esi+(ebp+3)*4+wc],ebp  ; wc(ebp+3)=ebp+2
  1695.          add dword ptr [esi+(ebp+0)*4+wc],3
  1696.          add dword ptr [esi+(ebp+2)*4+wc],1
  1697.          add dword ptr [esi+(ebp+3)*4+wc],2
  1698.  
  1699.          mov [esi+(ebp+0)*4+wg],ebp  ; wg(ebp)=ebp+1
  1700.          mov [esi+(ebp+1)*4+wg],ebp  ; wg(ebp+1)=ebp+2
  1701.          mov [esi+(ebp+2)*4+wg],ebp  ; wg(ebp+2)=ebp+3
  1702.          mov [esi+(ebp+3)*4+wg],ebp  ; wg(ebp+3)=ebp+0
  1703.          add dword ptr [esi+(ebp+0)*4+wg],1
  1704.          add dword ptr [esi+(ebp+1)*4+wg],2
  1705.          add dword ptr [esi+(ebp+2)*4+wg],3
  1706.  
  1707.          mov [esi+(ebp+0)*4+sd],edi  ; sd(ebp)=surface number
  1708.          mov [esi+(ebp+1)*4+sd],edi
  1709.          mov [esi+(ebp+2)*4+sd],edi
  1710.          mov [esi+(ebp+3)*4+sd],edi
  1711.  
  1712.          mov edi,thismaterial
  1713.          mov [esi+(ebp+0)*4+mt],edi  ; set material for surface
  1714.          mov [esi+(ebp+1)*4+mt],edi
  1715.          mov [esi+(ebp+2)*4+mt],edi
  1716.          mov [esi+(ebp+3)*4+mt],edi
  1717.  
  1718.          mov ebp,ecx
  1719.          mov ecx,ebx
  1720.          mov ebx,eax
  1721.          call acalc_normal
  1722.  
  1723.          mov edi,connections
  1724.          mov esi,connmem
  1725.  
  1726.          mov [esi+(edi+0)*4+ea],ebx
  1727.          mov [esi+(edi+0)*4+eb],ecx
  1728.          mov [esi+(edi+0)*4+ec],ebp
  1729.          mov [esi+(edi+0)*4+ed],edx
  1730.          mov [esi+(edi+1)*4+ea],ebx
  1731.          mov [esi+(edi+1)*4+eb],ecx
  1732.          mov [esi+(edi+1)*4+ec],ebp
  1733.          mov [esi+(edi+1)*4+ed],edx
  1734.          mov [esi+(edi+2)*4+ea],ebx
  1735.          mov [esi+(edi+2)*4+eb],ecx
  1736.          mov [esi+(edi+2)*4+ec],ebp
  1737.          mov [esi+(edi+2)*4+ed],edx
  1738.          mov [esi+(edi+3)*4+ea],ebx
  1739.          mov [esi+(edi+3)*4+eb],ecx
  1740.          mov [esi+(edi+3)*4+ec],ebp
  1741.          mov [esi+(edi+3)*4+ed],edx
  1742.  
  1743.          add surfaces,1
  1744.          add connections,4
  1745.          jmp dxf_3dfaces
  1746. dxf_line:
  1747.          mov [esi+(ebp+0)*4+p1],eax  ; p1(ebp)=eax
  1748.          mov [esi+(ebp+0)*4+p2],ebx  ; p2(ebp)=ebx
  1749.          mov [esi+(ebp+1)*4+p1],ebx  ; p1(ebp+1)=ebx
  1750.          mov [esi+(ebp+1)*4+p2],eax  ; p2(ebp+1)=ecx
  1751.  
  1752.          mov [esi+(ebp+0)*4+wc],ebp  ; wc(ebp)=ebp+1
  1753.          mov [esi+(ebp+1)*4+wc],ebp  ; wc(ebp+1)=ebp+0
  1754.          add dword ptr [esi+(ebp+0)*4+wc],1
  1755.  
  1756.          mov [esi+(ebp+0)*4+wg],ebp  ; wg(ebp)=ebp+1
  1757.          mov [esi+(ebp+1)*4+wg],ebp  ; wg(ebp+1)=ebp+0
  1758.          add dword ptr [esi+(ebp+0)*4+wg],1
  1759.  
  1760.          mov [esi+(ebp+0)*4+sd],edi  ; sd(ebp)=surface number
  1761.          mov [esi+(ebp+1)*4+sd],edi
  1762.  
  1763.          mov edi,thismaterial
  1764.          mov [esi+(ebp+0)*4+mt],edi  ; set material for surface
  1765.          mov [esi+(ebp+1)*4+mt],edi
  1766.  
  1767.          xor eax,eax
  1768.          mov edi,connections
  1769.          mov esi,connmem
  1770.  
  1771.          mov [esi+(edi+0)*4+ea],eax
  1772.          mov [esi+(edi+0)*4+eb],eax
  1773.          mov [esi+(edi+0)*4+ec],eax
  1774.          mov [esi+(edi+0)*4+ed],eax
  1775.          mov [esi+(edi+1)*4+ea],eax
  1776.          mov [esi+(edi+1)*4+eb],eax
  1777.          mov [esi+(edi+1)*4+ec],eax
  1778.          mov [esi+(edi+1)*4+ed],eax
  1779.  
  1780.          add surfaces,1
  1781.          add connections,2
  1782.  
  1783.          jmp dxf_3dfaces
  1784.  
  1785. dxf_point:
  1786.          mov [esi+(ebp+0)*4+p1],eax  ; p1(ebp)=eax
  1787.          mov [esi+(ebp+0)*4+p2],eax  ; p2(ebp)=ebx
  1788.  
  1789.          mov [esi+(ebp+0)*4+wc],ebp  ; wc(ebp)=ebp+0
  1790.          mov [esi+(ebp+0)*4+wc],ebp  ; wc(ebp+1)=ebp+0
  1791.  
  1792.          mov [esi+(ebp+0)*4+wg],ebp  ; wg(ebp)=ebp+0
  1793.          mov [esi+(ebp+1)*4+wg],ebp  ; wg(ebp+1)=ebp+0
  1794.  
  1795.          mov [esi+(ebp+0)*4+sd],edi  ; sd(ebp)=surface number
  1796.  
  1797.          mov edi,thismaterial
  1798.          mov [esi+(ebp+0)*4+mt],edi  ; set material for surface
  1799.  
  1800.          xor eax,eax
  1801.          mov edi,connections
  1802.          mov esi,connmem
  1803.  
  1804.          mov [esi+(edi+0)*4+ea],eax
  1805.          mov [esi+(edi+0)*4+eb],eax
  1806.          mov [esi+(edi+0)*4+ec],eax
  1807.          mov [esi+(edi+0)*4+ed],eax
  1808.  
  1809.          add surfaces,1
  1810.          add connections,1
  1811.  
  1812.          jmp dxf_3dfaces
  1813.  
  1814. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1815. ; Input Polyline PFACEs
  1816. ; In:
  1817. ;   fileloc => location to start checking for 3dfaces
  1818. ;   filetill => location to end checking for 3dfaces
  1819. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1820.  
  1821. tag3     db "POLYLINE",0
  1822. tag4     db "VERTEX",0
  1823. tag70    db "70",0
  1824. tag71    db "71",0
  1825. tag72    db "72",0
  1826. tag73    db "73",0
  1827. tag74    db "74",0
  1828.  
  1829. dxf_polylines:
  1830.          mov edx,offset tag3
  1831.          mov edi,fileloc
  1832.          mov ebp,facetill
  1833.          call search_string
  1834.          jc _ret
  1835.  
  1836.          mov edx,offset tag2
  1837.          mov ebp,facetill
  1838.          call search_string
  1839.          jc dxf_polylines     ; no layer to polyline?
  1840.  
  1841.          mov edx,edi
  1842.          call next
  1843.          mov fileloc,edx
  1844.          call assign_material
  1845.          mov thismaterial,eax
  1846.  
  1847.          cmp layer,no
  1848.          je dxf_pnosel
  1849.          mov ecx,eax
  1850.          call find_info
  1851.          cmp edx,offset fidefault
  1852.          je dxf_polylines
  1853.  
  1854. dxf_pnosel:
  1855.          mov edx,fileloc
  1856.          call next
  1857.          mov edi,edx
  1858.          mov edx,offset tag70
  1859.          mov ebp,facetill
  1860.          call search_string
  1861.          jc exiterr6
  1862.  
  1863.          mov edx,edi
  1864.          call next
  1865.          call _strhtn
  1866.          call _vct16
  1867.          cmp eax,64
  1868.          jne dxf_polylines
  1869.  
  1870.          mov edx,fileloc
  1871.          call next
  1872.          mov edi,edx
  1873.          mov edx,offset tag71
  1874.          mov ebp,facetill
  1875.          call search_string
  1876.          jc exiterr6
  1877.  
  1878.          mov edx,edi
  1879.          call next
  1880.          call _strhtn
  1881.          call _vct32
  1882.          mov pvertexs,eax
  1883.  
  1884.          mov edx,fileloc
  1885.          call next
  1886.          mov edi,edx
  1887.          mov edx,offset tag72
  1888.          mov ebp,facetill
  1889.          call search_string
  1890.          jc exiterr6
  1891.  
  1892.          mov edx,edi
  1893.          call next
  1894.          mov fileloc,edx
  1895.          call _strhtn
  1896.          call _vct32
  1897.          mov pfaces,eax
  1898.          mov temp2,eax
  1899.  
  1900.          mov temp1,1
  1901. dxfpv_load:
  1902.          mov edi,fileloc
  1903.          mov edx,offset tag4
  1904.          mov ebp,facetill
  1905.          call search_string
  1906.          jc exiterr6
  1907.  
  1908.          mov fileloc,edi
  1909.          mov edx,offset tagx
  1910.          call getnumber
  1911.          add eax,nx
  1912.          mov ebx,scale
  1913.          imul32 ebx
  1914.          add eax,nu
  1915.          push eax
  1916.  
  1917.          mov edx,offset tagy
  1918.          call getnumber
  1919.          add eax,ny
  1920.          mov ebx,scale
  1921.          imul32 ebx
  1922.          add eax,nw
  1923.          push eax
  1924.  
  1925.          mov edx,offset tagz
  1926.          call getnumber
  1927.          add eax,nz
  1928.          mov ebx,scale
  1929.          imul32 ebx
  1930.          add eax,nv
  1931.          cmp yneg,yes
  1932.          je noynegp
  1933.          neg eax
  1934. noynegp:
  1935.          mov ecx,eax
  1936.          pop ebp
  1937.          pop ebx
  1938.          call check_point
  1939.  
  1940.          mov edi,temp1
  1941.          mov polyindex[edi*4],eax
  1942.          inc temp1
  1943.          mov eax,temp1
  1944.          cmp eax,pvertexs
  1945.          jbe dxfpv_load
  1946. dxf_pmore:
  1947.          mov edi,fileloc       ; now reconstruct polyline into face
  1948.          mov edx,offset tag4
  1949.          mov ebp,facetill
  1950.          call search_string
  1951.          jc exiterr6
  1952.  
  1953.          mov edx,offset tag71
  1954.          call getnumber
  1955.          shr eax,16
  1956.  
  1957.          mov ebp,connections    ; set first point in poly
  1958.          mov startconn,ebp
  1959.          mov eax,[polyindex+eax*4]
  1960.          mov start,eax
  1961.          mov esi,connmem
  1962.          mov [esi+ebp*4+p1],eax
  1963.  
  1964.          mov edx,offset tag72
  1965.          call getnumber
  1966.          jc exiterr6
  1967.          shr eax,16
  1968.          movsx eax,ax
  1969.          mov temp3,0
  1970.          cmp eax,0
  1971.          jge pf_kkl
  1972.          neg eax
  1973.          mov temp3,1
  1974. pf_kkl:
  1975.          mov ebp,[polyindex+eax*4]
  1976.          cmp ebp,start
  1977.          je pf_close
  1978.          call addpoint
  1979.          cmp temp3,1
  1980.          je pf_close
  1981.  
  1982.          mov edx,offset tag73
  1983.          call getnumber
  1984.          jc pf_close
  1985.          shr eax,16
  1986.          movsx eax,ax
  1987.          mov temp3,0
  1988.          cmp eax,0
  1989.          jge pf_kkq
  1990.          neg eax
  1991.          mov temp3,1
  1992. pf_kkq:
  1993.          mov ebp,[polyindex+eax*4]
  1994.          cmp ebp,start
  1995.          je pf_close
  1996.          call addpoint
  1997.          cmp temp3,1
  1998.          je pf_close
  1999.  
  2000.          mov temp4,0
  2001.          mov edx,offset tag74
  2002.          call getnumber
  2003.          jc pf_close
  2004.          mov temp4,1   ; temp4 = 1 means calc normal
  2005.          shr eax,16
  2006.          movsx eax,ax
  2007.  
  2008.          mov temp3,0
  2009.          cmp eax,0
  2010.          jge pf_negit
  2011.          neg eax
  2012.          mov temp3,1
  2013. pf_negit:
  2014.          mov ebp,[polyindex+eax*4]
  2015.          cmp ebp,start
  2016.          je pf_close
  2017.          call addpoint
  2018.          cmp temp3,0
  2019.          je pf_close
  2020. pf_multi:
  2021.          mov edi,fileloc
  2022.          mov edx,offset tag4
  2023.          mov ebp,facetill
  2024.          call search_string
  2025.          jc exiterr6
  2026.  
  2027.          mov edx,offset tag73
  2028.          call getnumber
  2029.          jc exiterr6
  2030.          shr eax,16
  2031.          movsx eax,ax
  2032.  
  2033.          mov temp3,0
  2034.          cmp eax,0
  2035.          jge pf_negitp
  2036.          neg eax
  2037.          mov temp3,1
  2038. pf_negitp:
  2039.          mov ebp,[polyindex+eax*4]
  2040.          cmp ebp,start
  2041.          je pf_close
  2042.          call addpoint
  2043.          cmp temp3,1
  2044.          je pf_multi
  2045. pf_close:
  2046.          mov eax,startconn
  2047.          mov ebp,connections
  2048.          mov esi,connmem
  2049.          mov [esi+eax*4+wc],ebp
  2050.          mov [esi+ebp*4+wg],eax
  2051.          mov ebx,start
  2052.          mov [esi+ebp*4+p2],ebx
  2053.          mov eax,thismaterial
  2054.          mov [esi+ebp*4+mt],eax
  2055.          mov eax,surfaces
  2056.          mov [esi+ebp*4+sd],eax
  2057.  
  2058.          cmp temp4,0
  2059.          je pf_nnor
  2060.  
  2061.          mov esi,connmem
  2062.          mov edx,startconn
  2063.          mov ebx,[esi+(edx+0)*4+p1]
  2064.          mov ecx,[esi+(edx+0)*4+p2]
  2065.          mov ebp,[esi+(edx+1)*4+p2]
  2066.          call acalc_normal
  2067.  
  2068.          mov esi,connmem
  2069.          mov eax,startconn
  2070. pf_applyl:
  2071.          mov [esi+(eax+0)*4+ea],ebx
  2072.          mov [esi+(eax+0)*4+eb],ecx
  2073.          mov [esi+(eax+0)*4+ec],ebp
  2074.          mov [esi+(eax+0)*4+ed],edx
  2075.          mov eax,[esi+(eax+0)*4+wg]
  2076.          cmp eax,startconn
  2077.          jne pf_applyl
  2078.  
  2079. pf_nnor:
  2080.          inc connections
  2081.          inc surfaces
  2082.  
  2083.          dec pfaces
  2084.          jnz dxf_pmore
  2085.  
  2086.          jmp dxf_polylines
  2087. addpoint:
  2088.          mov ebp,connections
  2089.          mov eax,[polyindex+eax*4]
  2090.          mov esi,connmem
  2091.          mov [esi+ebp*4+p2],eax
  2092.          mov [esi+(ebp+1)*4+p1],eax
  2093.          mov [esi+ebp*4+wg],ebp
  2094.          inc dword ptr [esi+ebp*4+wg]
  2095.          mov eax,surfaces
  2096.          mov [esi+ebp*4+sd],eax
  2097.          mov eax,thismaterial
  2098.          mov [esi+ebp*4+mt],eax
  2099.          inc connections
  2100.          ret
  2101.  
  2102. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2103. ; Find opposite connection for line EBP
  2104. ; In:  EBX = connection to find pair for
  2105. ; Out: CF = 1 not found,  CF = 0 connection found at EDI!
  2106. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2107.  
  2108. find_pair:
  2109.          mov esi,connmem
  2110.          mov ecx,[esi+ebp*4+p1]  ; eg have (7,9) look for (9,7)
  2111.          mov edx,[esi+ebp*4+p2]
  2112.          mov eax,ecx
  2113.          or eax,edx
  2114.          jz fpnomatch
  2115.          xor edi,edi
  2116. fploop:
  2117.          mov eax,[esi+edi*4+p2]
  2118.          mov ebx,[esi+edi*4+p1]
  2119.  
  2120.          cmp eax,ecx
  2121.          jne fpnotmatch0
  2122.          cmp ebx,edx
  2123.          jne fpnotmatch0         ; backwards points?
  2124.  
  2125.          mov eax,[esi+ebp*4+ea]  ; yes, test if same plane equation
  2126.          cmp [esi+edi*4+ea],eax
  2127.          jne fpnotmatch1
  2128.          mov eax,[esi+ebp*4+eb]
  2129.          cmp [esi+edi*4+eb],eax
  2130.          jne fpnotmatch1
  2131.          mov eax,[esi+ebp*4+ec]
  2132.          cmp [esi+edi*4+ec],eax
  2133.          jne fpnotmatch1
  2134.          mov eax,[esi+ebp*4+ed]
  2135.          cmp [esi+edi*4+ed],eax
  2136.          jne fpnotmatch1
  2137.          mov eax,[esi+ebp*4+mt]  ; same equation, test if same material
  2138.          cmp [esi+edi*4+mt],eax
  2139.          jne fpnotmatch1
  2140.  
  2141.          clc
  2142.          ret
  2143. fpnotmatch0:
  2144.          or eax,ebx
  2145.          jz fpnomatch
  2146.  
  2147. fpnotmatch1:
  2148.          inc edi
  2149.          cmp edi,maxpoints
  2150.          jl fploop
  2151. fpnomatch:
  2152.          mov edi,-1
  2153.          stc
  2154.          ret
  2155.  
  2156. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2157. ; Renumber surfaces
  2158. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2159. renumber_surfaces:
  2160.          xor ebx,ebx
  2161. rnloop:
  2162.          call rn_lowest
  2163.          jc _ret
  2164.          call rn_eax2ebx
  2165.          inc ebx
  2166.          jmp rnloop
  2167.  
  2168. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2169. ; Search for lowest surface number (but above EBX)
  2170. ; In:  EBX = lowest surface number to test
  2171. ; Out:
  2172. ;  CF = 1, not found.
  2173. ;  CF = 0
  2174. ;   EAX = lowest surface number
  2175. ;   EBP = connection number where found
  2176. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2177.  
  2178. rn_lowest:
  2179.          mov esi,connmem
  2180.          mov ecx,connections
  2181.          mov eax,-1
  2182. rnlloop:
  2183.          mov edx,[esi+(ecx-1)*4+sd]
  2184.          cmp ebx,edx
  2185.          ja rnlnotel
  2186.  
  2187.          cmp eax,edx
  2188.          jb rnlnotel
  2189.  
  2190.          mov eax,edx
  2191.          mov ebp,ecx
  2192.          dec ebp
  2193. rnlnotel:
  2194.          loop rnlloop
  2195.  
  2196.          inc eax
  2197.          sub eax,1
  2198.          ret
  2199.  
  2200. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2201. ; Renumber surfaces EAX to EBX
  2202. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2203.  
  2204. rn_eax2ebx:
  2205.          mov esi,connmem
  2206.          lea edi,[esi+sd]
  2207.          mov ecx,connections
  2208. rn_eaxl:
  2209.          repnz scasd
  2210.  
  2211.          cmp ecx,0
  2212.          jz _ret
  2213.  
  2214.          mov [edi-4],ebx
  2215.          jmp rn_eaxl
  2216.  
  2217. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2218. ; Delete unused points and collect
  2219. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2220. collect_points:
  2221.          mov esi,connmem
  2222.          mov eax,1
  2223. coploop:
  2224.          lea edi,[esi+p1]
  2225.          mov ecx,connections
  2226.          repnz scasd
  2227.          sub ecx,1
  2228.          jnc cpused
  2229.  
  2230.          lea edi,[esi+p2]
  2231.          mov ecx,connections
  2232.          repnz scasd
  2233.          sub ecx,1
  2234.          jnc cpused
  2235.  
  2236.          call cpdeleteit
  2237. cpused:
  2238.          inc eax
  2239.          cmp eax,points
  2240.          jne coploop
  2241.  
  2242.          ret
  2243.  
  2244. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2245. ; Delete point EAX
  2246. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2247. cpdeleteit:
  2248.          mov ebp,points
  2249.          mov esi,connmem
  2250.          lea ecx,[ebp-eax]
  2251.          lea edi,[esi+(eax+0)*4+ox]
  2252.          lea esi,[esi+(eax+1)*4+ox]
  2253.          rep movsd
  2254.  
  2255.          mov esi,connmem
  2256.          lea ecx,[ebp-eax]
  2257.          lea edi,[esi+(eax+0)*4+oy]
  2258.          lea esi,[esi+(eax+1)*4+oy]
  2259.          rep movsd
  2260.  
  2261.          mov esi,connmem
  2262.          lea ecx,[ebp-eax]
  2263.          lea edi,[esi+(eax+0)*4+oz]
  2264.          lea esi,[esi+(eax+1)*4+oz]
  2265.          rep movsd
  2266.  
  2267.          mov esi,connmem
  2268.          mov ecx,connections
  2269.          xor ebp,ebp
  2270. cpdloop:
  2271.          mov ebx,[esi+ebp*4+p1]
  2272.          cmp ebx,eax
  2273.          jb cpskip1
  2274.          dec dword ptr [esi+ebp*4+p1]
  2275. cpskip1:
  2276.          mov ebx,[esi+ebp*4+p2]
  2277.          cmp ebx,eax
  2278.          jb cpskip2
  2279.          dec dword ptr [esi+ebp*4+p2]
  2280. cpskip2:
  2281.          inc ebp
  2282.          loop cpdloop
  2283.  
  2284.          dec points
  2285.          ret
  2286.  
  2287. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2288. ; Wipe D value from plane equation if no iteration used
  2289. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2290. wipe_d:
  2291.          cmp iterate,yes
  2292.          je _ret
  2293.          mov esi,connmem
  2294.          mov ecx,connections
  2295.          dec ecx
  2296.          xor edx,edx
  2297. wdkl:
  2298.          mov [esi+(ecx)*4+ed],edx
  2299.          loop wdkl
  2300.  
  2301.          mov [esi+(ecx)*4+ed],edx
  2302.          ret
  2303.  
  2304. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2305. ; Output file - file is opened and ready
  2306. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2307.  
  2308. output_file:
  2309.          mov ecx,40                    ; output header info
  2310.          mov al,"."
  2311.          mov edi,offset outputname
  2312.          repnz scasb
  2313.          mov byte ptr [edi-1],0
  2314.          sub edi,offset outputname
  2315.          push edi
  2316.          push edi
  2317.  
  2318.          mov edx,offset header0
  2319.          call _write_null
  2320.          mov edx,offset header1
  2321.          call _write_null
  2322.          mov edx,offset outputname
  2323.          call _write_null
  2324.          pop ecx
  2325.          mov edx,offset header7
  2326.          add edx,ecx
  2327.          call _write_null
  2328.          mov edx,offset header2
  2329.          call _write_null
  2330.          mov edx,offset outputname
  2331.          call _write_null
  2332.          mov edx,offset header3
  2333.          call _write_null
  2334.          mov edx,offset outputname
  2335.          call _write_null
  2336.          pop ecx
  2337.          mov edx,offset header4
  2338.          add edx,ecx
  2339.          call _write_null
  2340.          mov eax,points
  2341.          call _write_dec16
  2342.          mov eax,","
  2343.          call _write_string4
  2344.          mov eax,surfaces
  2345.          call _write_dec16
  2346.          call _write_ret
  2347.  
  2348.          mov edx,offset header4
  2349.          call _write_null
  2350.          mov edx,offset header5
  2351.          call _write_null
  2352.          call _write_ret
  2353.          call _write_ret
  2354.  
  2355.          mov temp1,1                ; now output points
  2356. of_loop1:
  2357.          mov edx,offset header4
  2358.          call _write_null
  2359.  
  2360.          mov esi,connmem
  2361.          mov ecx,temp1
  2362.          mov eax,[esi+ecx*4+ox]
  2363.          mov edx,offset buffer
  2364.          call _write_neg16
  2365.          call _write_null
  2366.          mov eax,","
  2367.          call _write_string4
  2368.          mov esi,connmem
  2369.          mov ecx,temp1
  2370.          mov eax,[esi+ecx*4+oy]
  2371.          mov edx,offset buffer
  2372.          call _write_neg16
  2373.          call _write_null
  2374.          mov eax,","
  2375.          call _write_string4
  2376.          mov esi,connmem
  2377.          mov ecx,temp1
  2378.          mov eax,[esi+ecx*4+oz]
  2379.          mov edx,offset buffer
  2380.          call _write_neg16
  2381.          call _write_null
  2382.          mov edx,offset header6
  2383.          call _write_null
  2384.          mov eax,temp1
  2385.          call _write_dec16
  2386.          call _write_ret
  2387.          inc temp1
  2388.          mov ecx,temp1
  2389.          cmp ecx,points
  2390.          jbe of_loop1
  2391.          call _write_ret
  2392.  
  2393.          mov temp1,0              ; output surfaces
  2394. dp_sloop:
  2395.          mov ebx,temp1
  2396.          call rn_lowest
  2397.          mov temp2,ebp
  2398.  
  2399.          mov edx,offset header4
  2400.          call _write_null
  2401.          mov esi,connmem
  2402.          mov ecx,temp2
  2403.          mov ecx,[esi+ecx*4+mt]
  2404.          call find_info
  2405.          mov thismaterial,edx
  2406.          mov edx,thismaterial
  2407.          call find_shade
  2408.          jc dp_noshade0
  2409.          mov esi,connmem
  2410.          mov ebp,temp2
  2411.          mov ebp,[esi+ebp*4+wg]
  2412.          mov ebp,[esi+ebp*4+wg]
  2413.          cmp ebp,temp2
  2414.          jne dp_noshade0
  2415.          mov edx,offset fidefaull
  2416.          mov thismaterial,edx
  2417. dp_noshade0:
  2418.          mov edx,thismaterial
  2419.          call change_underscore ; if direct copy used (underscore), change to ","
  2420.          call write_mat
  2421.          mov eax,","
  2422.          call _write_string4
  2423.  
  2424.          mov edx,thismaterial
  2425.          call find_map
  2426.          jc dp_nobitmap
  2427.  
  2428.          mov ebp,temp2
  2429.          mov esi,connmem
  2430.          mov eax,[esi+ebp*4+p1]
  2431.          call _write_dec16
  2432.          jmp dp_noshade
  2433.  
  2434. dp_nobitmap:
  2435.          mov ebp,temp2
  2436.          mov esi,connmem
  2437.          mov eax,[esi+ebp*4+p1]
  2438.          call _write_dec16
  2439. dp_cloop:
  2440.          mov temp3,ebp
  2441.          mov eax,","
  2442.          call _write_string4
  2443.          mov esi,connmem
  2444.          mov ebp,temp3
  2445.          mov eax,[esi+ebp*4+p2]
  2446.          call _write_dec16
  2447.          mov esi,connmem
  2448.          mov ebp,temp3
  2449.          mov ebp,[esi+ebp*4+wg]
  2450.          cmp temp2,ebp
  2451.          jne dp_cloop
  2452.  
  2453.          mov edx,thismaterial
  2454.          call find_shade
  2455.          jc dp_noshade
  2456.  
  2457.          mov esi,connmem
  2458.          mov ebp,temp2
  2459.          mov ebp,[esi+ebp*4+wg]
  2460.          mov ebp,[esi+ebp*4+wg]
  2461.          cmp ebp,temp2
  2462.          je dp_noshade
  2463.  
  2464.          cmp tnormal,no
  2465.          je normalnormal
  2466.          mov eax,","
  2467.          call _write_string4
  2468.          mov esi,connmem
  2469.          mov ebp,temp2
  2470.          mov eax,[esi+ebp*4+ea]
  2471.          shr eax,2
  2472.          mov edx,offset buffer
  2473.          call _write_neg16
  2474.          call unpad
  2475.          call _write_null
  2476.          mov eax,","
  2477.          call _write_string4
  2478.          mov esi,connmem
  2479.          mov ebp,temp2
  2480.          mov eax,[esi+ebp*4+eb]
  2481.          shr eax,2
  2482.          mov edx,offset buffer
  2483.          call _write_neg16
  2484.          call unpad
  2485.          call _write_null
  2486.          mov eax,","
  2487.          call _write_string4
  2488.          mov esi,connmem
  2489.          mov ebp,temp2
  2490.          mov eax,[esi+ebp*4+ec]
  2491.          shr eax,2
  2492.          mov edx,offset buffer
  2493.          call _write_neg16
  2494.          call unpad
  2495.          call _write_null
  2496.          jmp dp_noshade
  2497. normalnormal:
  2498.          mov edx,offset header9
  2499.          call _write_null
  2500.  
  2501. dp_noshade:
  2502.          call write_colon
  2503.          call _write_ret
  2504.  
  2505.          inc temp1
  2506.          mov ecx,temp1
  2507.          cmp ecx,surfaces
  2508.          jne dp_sloop
  2509.  
  2510.          call _write_ret
  2511.          ret
  2512.  
  2513. header0  db ";DXF23DV Converter V0.5 - This conversion conforms to version 3DVECT35",13,10,13,10,0
  2514. header1  db "h",0
  2515. header2  db "-1",13,10,"           dd offset o",0
  2516. header3  db " - offset $ - 4",13,10,13,10,"o",0
  2517. header4  db "           dw ",0
  2518. header5  db "25 dup (0)",0
  2519. header6  db "  ; point ",0
  2520. header7  db "           dd ",0
  2521. header8  db " offset o",0
  2522. header9  db ",0,0,0",0
  2523.  
  2524. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2525. ; Find if input name is a DXF or a PLG
  2526. ; Defaults to DXF
  2527. ; Out: CF = 1, PLG      CF = 0, DXF
  2528. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2529.  
  2530. isitadxforplg:
  2531.          mov ecx,40                    ; output header info
  2532.          mov al,"."
  2533.          mov edi,offset inputname
  2534. isloop:
  2535.          inc edi
  2536.          cmp [edi-1],al
  2537.          loopne isloop
  2538.          mov al,byte ptr [edi+0]
  2539.          upper al
  2540.          cmp al,"P"
  2541.          jne itsadxf
  2542.          mov al,byte ptr [edi+1]
  2543.          upper al
  2544.          cmp al,"L"
  2545.          jne itsadxf
  2546.          mov al,byte ptr [edi+2]
  2547.          upper al
  2548.          cmp al,"G"
  2549.          jne itsadxf
  2550.          stc
  2551.          ret
  2552. itsadxf:
  2553.          clc
  2554.          ret
  2555.  
  2556. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2557. ; Input PLG file
  2558. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2559.  
  2560. plg_getall:
  2561.          mov edx,dxfo
  2562. plg_unpad:
  2563.          call unpad
  2564.          mov al,[edx]
  2565.          cmp al,"9"
  2566.          jbe plg_start
  2567.          call next
  2568.          jmp plg_unpad
  2569.  
  2570. plg_start:
  2571.          mov fileloc,edx
  2572.          call _strhtn
  2573.          call _vct32
  2574.          mov temp2,eax
  2575.  
  2576.          mov edx,fileloc
  2577.          call next
  2578.          call _strhtn
  2579.          call _vct32
  2580.          mov surfaces,eax
  2581.  
  2582.          mov edx,fileloc
  2583.          call next
  2584.          call next
  2585.          mov fileloc,edx
  2586.  
  2587.          mov temp1,0         ; begin loading of points
  2588. plg_points:
  2589.          mov edx,fileloc
  2590.          call _get_float32
  2591.          add eax,nx
  2592.          mov ebx,scale
  2593.          imul32 ebx
  2594.          add eax,nu
  2595.          push eax
  2596.  
  2597.          mov edx,fileloc
  2598.          call next
  2599.          mov fileloc,edx
  2600.          call _get_float32
  2601.          add eax,ny
  2602.          mov ebx,scale
  2603.          imul32 ebx
  2604.          add eax,nv
  2605.          cmp yneg,yes
  2606.          je noynegt
  2607.          neg eax
  2608. noynegt:
  2609.          push eax
  2610.  
  2611.          mov edx,fileloc
  2612.          call next
  2613.          mov fileloc,edx
  2614.          call _get_float32
  2615.          add eax,nz
  2616.          mov ebx,scale
  2617.          imul32 ebx
  2618.          add eax,nw
  2619.          push eax
  2620.  
  2621.          mov edx,fileloc
  2622.          call next
  2623.          mov fileloc,edx
  2624.  
  2625.          pop ecx
  2626.          pop ebx
  2627.          pop ebp
  2628.          call check_point     ; add point to list
  2629.  
  2630.          mov edi,temp1
  2631.          mov polyindex[edi*4],eax
  2632.          inc temp1
  2633.          dec temp2
  2634.          jnz plg_points
  2635.  
  2636.          mov eax,surfaces     ; begin loading of surfaces
  2637.          mov temp2,eax
  2638.          xor eax,eax
  2639.          mov surfaces,eax
  2640.  
  2641. plg_pmore:
  2642.          mov edx,fileloc      ; get material.  eg: 0x8001
  2643.          call assign_material
  2644.          mov thismaterial,eax
  2645.  
  2646.          mov edx,fileloc
  2647.          call next
  2648.          mov fileloc,edx
  2649.  
  2650.          call _strhtn         ; number of connections in list
  2651.          call _vct32
  2652.          mov temp3,eax
  2653.          mov temp4,eax
  2654.  
  2655.          mov edx,fileloc
  2656.          call next
  2657.          mov fileloc,edx
  2658.          call _strhtn
  2659.          call _vct32
  2660.  
  2661.          mov start,eax
  2662.          mov ebp,connections    ; set first point in poly
  2663.          mov startconn,ebp
  2664.          mov eax,[polyindex+eax*4]
  2665.          mov esi,connmem
  2666.          mov [esi+ebp*4+p1],eax
  2667.  
  2668.          cmp temp3,1
  2669.          je plg_close
  2670.          dec temp3
  2671. plg_ll:
  2672.          mov edx,fileloc
  2673.          call next
  2674.          mov fileloc,edx
  2675.          call _strhtn
  2676.          call _vct32
  2677.  
  2678.          mov ecx,[polyindex+eax*4]
  2679.          mov ebp,start
  2680.          cmp ecx,[polyindex+ebp*4]
  2681.          je plg_closez
  2682.  
  2683.          call addpoint
  2684.          dec temp3
  2685.          jnz plg_ll
  2686.          jmp plg_close
  2687.  
  2688. plg_closez:
  2689.          dec temp3
  2690.          jz plg_close
  2691.          mov edx,fileloc
  2692.          call next
  2693.          mov fileloc,edx
  2694.          jmp plg_closez
  2695.  
  2696. plg_close:
  2697.          mov edx,fileloc
  2698.          call next
  2699.          mov fileloc,edx
  2700.  
  2701.          mov eax,startconn
  2702.          mov ebp,connections
  2703.          mov esi,connmem
  2704.          mov [esi+eax*4+wc],ebp
  2705.          mov [esi+ebp*4+wg],eax
  2706.          mov ebx,start
  2707.          mov ebx,[polyindex+ebx*4]
  2708.          mov [esi+ebp*4+p2],ebx
  2709.          mov eax,thismaterial
  2710.          mov [esi+ebp*4+mt],eax
  2711.          mov eax,surfaces
  2712.          mov [esi+ebp*4+sd],eax
  2713.  
  2714.          cmp temp4,2
  2715.          jle plg_nnor
  2716.  
  2717.          mov esi,connmem
  2718.          mov edx,startconn
  2719.          mov ebx,[esi+(edx+0)*4+p1]
  2720.          mov ecx,[esi+(edx+0)*4+p2]
  2721.          mov ebp,[esi+(edx+1)*4+p2]
  2722.          call acalc_normal
  2723.  
  2724.          mov esi,connmem
  2725.          mov eax,startconn
  2726. plg_applyl:
  2727.          mov [esi+(eax+0)*4+ea],ebx
  2728.          mov [esi+(eax+0)*4+eb],ecx
  2729.          mov [esi+(eax+0)*4+ec],ebp
  2730.          mov [esi+(eax+0)*4+ed],edx
  2731.          mov eax,[esi+(eax+0)*4+wg]
  2732.          cmp eax,startconn
  2733.          jbe plg_applyl
  2734.  
  2735. plg_nnor:
  2736.          inc connections
  2737.          inc surfaces
  2738.  
  2739.          cmp layer,no
  2740.          je plg_pnosel
  2741.          mov ecx,thismaterial
  2742.          call find_info
  2743.          cmp edx,offset fidefault
  2744.          jne plg_pnosel
  2745.  
  2746.          mov ecx,startconn
  2747.          mov esi,connmem
  2748.          xor eax,eax
  2749. plg_clrl:
  2750.          mov [esi+(ecx+0)*4+ea],eax
  2751.          mov [esi+(ecx+0)*4+eb],eax
  2752.          mov [esi+(ecx+0)*4+ec],eax
  2753.          mov [esi+(ecx+0)*4+ed],eax
  2754.          inc ecx
  2755.          cmp ecx,connections
  2756.          jne plg_clrl
  2757.  
  2758.          mov ecx,startconn
  2759.          mov connections,ecx
  2760.          dec surfaces
  2761.  
  2762. plg_pnosel:
  2763.          dec temp2
  2764.          jnz plg_pmore
  2765.  
  2766.          ret
  2767.  
  2768. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2769. ; Randomize number
  2770. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2771.  
  2772. _randomize:
  2773.          xor eax,randomnumber
  2774.          xor eax,edx
  2775.          xchg al,ah
  2776.          add eax,0cd9c9a8fh
  2777.          xor eax,esi
  2778.          add eax,edi
  2779.          xor eax,0526dafb2h
  2780.          add eax,ecx
  2781.          xor eax,ebx
  2782.          add eax,ebp
  2783.          mov randomnumber,eax
  2784.          in al,64
  2785.          shl eax,8
  2786.          in al,65
  2787.          shl eax,8
  2788.          in al,64
  2789.          shl eax,8
  2790.          in al,64
  2791.          add randomnumber,eax
  2792.          mov eax,randomnumber
  2793.          ret
  2794.  
  2795. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2796. ; Get 8 bit random number from all random bits
  2797. ; In: Randomnumber
  2798. ; Out: AL = 8 bit result of all 32 random bits
  2799. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2800.  
  2801. _random8:
  2802.          mov eax,randomnumber
  2803.          xor ah,al
  2804.          shr eax,8
  2805.          xor ah,al
  2806.          shr eax,8
  2807.          xor ah,al
  2808.          shr eax,8
  2809.          ret
  2810.  
  2811. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2812. ; Find if "rnd" is on line
  2813. ; In: EDX => ASCIIZ string
  2814. ; Out: ECX => rnd   EDX = EDX
  2815. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2816.  
  2817. find_rnd:
  2818.          mov ecx,edx
  2819. find_rndq:
  2820.          mov al,[ecx]
  2821.          cmp al,0
  2822.          je fsnornd
  2823.          inc ecx
  2824.  
  2825.          upper al
  2826.          cmp al,"R"
  2827.          jne find_rndq
  2828.          mov al,[ecx+0]
  2829.          upper al
  2830.          cmp al,"N"
  2831.          jne find_rndq
  2832.          mov al,[ecx+1]
  2833.          upper al
  2834.          cmp al,"D"
  2835.          jne find_rndq
  2836.  
  2837.          dec ecx
  2838.          clc
  2839.          ret
  2840. fsnornd:
  2841.          stc
  2842.          ret
  2843.  
  2844. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2845. ; Find if a colon ":" is on line
  2846. ; In: EDX => ASCIIZ string
  2847. ; Out:
  2848. ;   CF = 1 - not found
  2849. ;    ECX => end of line
  2850. ;    EAX = 0
  2851. ;    EDX = EDX
  2852. ;   CF = 0 - found
  2853. ;    ECX => ":"
  2854. ;    EAX => ":"
  2855. ;    EDX = EDX
  2856. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2857.  
  2858. find_col:
  2859.          mov ecx,edx
  2860. find_colq:
  2861.          mov al,[ecx]
  2862.          cmp al,0
  2863.          je fsnocol
  2864.          inc ecx
  2865.  
  2866.          upper al
  2867.          cmp al,":"
  2868.          jne find_colq
  2869.  
  2870.          dec ecx
  2871.          mov eax,ecx
  2872.          clc
  2873.          ret
  2874. fsnocol:
  2875.          xor eax,eax
  2876.          stc
  2877.          ret
  2878.  
  2879. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2880. ; Write material to file:
  2881. ;   check for occurance of RND - place number of found
  2882. ;   check for occurance of colon - place 0 where found, copy to buffer
  2883. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2884.  
  2885. temp5    dd 0
  2886.  
  2887. write_mat:
  2888.          mov temp5,0
  2889.          call find_rnd
  2890.          jc _write_coln
  2891. _write_nullx:
  2892.          mov al,[ecx]
  2893.          push ax
  2894.          mov al,[ecx+1]
  2895.          push ax
  2896.          mov al,[ecx+2]
  2897.          push ax
  2898.          push ecx
  2899.          push edx
  2900.          push ecx
  2901.  
  2902.          call _randomize
  2903.          call _random8
  2904.          call _cv16
  2905.  
  2906.          pop ecx
  2907.          push eax
  2908.          and al,0fh
  2909.          add al,"0"
  2910.          mov [ecx+2],al
  2911.          pop eax
  2912.          push eax
  2913.          shr eax,4
  2914.          and al,0fh
  2915.          add al,"0"
  2916.          mov [ecx+1],al
  2917.          pop eax
  2918.          shr eax,8
  2919.          and al,0fh
  2920.          add al,"0"
  2921.          mov [ecx+0],al
  2922.  
  2923.          pop edx
  2924.          inc temp5
  2925.          call find_rnd
  2926.          jnc _write_nullx
  2927.          call _write_coln
  2928. wri_nx:
  2929.          pop ecx
  2930.          pop ax
  2931.          mov [ecx+2],al
  2932.          pop ax
  2933.          mov [ecx+1],al
  2934.          pop ax
  2935.          mov [ecx],al
  2936.          dec temp5
  2937.          jnz wri_nx
  2938.  
  2939.          ret
  2940.  
  2941. _write_coln:
  2942.          call find_col
  2943.          mov colon,eax
  2944.          mov byte ptr [ecx],0
  2945.          jmp _write_null
  2946.  
  2947. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2948. ; Write out color buffer and restore material (if colon <>0)
  2949. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2950.  
  2951. write_colon:
  2952.          mov edx,colon
  2953.          or edx,edx
  2954.          jz wc_nodata
  2955.  
  2956.          mov byte ptr [edx],":"
  2957.          inc edx
  2958.          call _write_null
  2959.  
  2960. wc_nodata:
  2961.          ret
  2962.  
  2963. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2964. ; Sort_them:  Sort surfaces based on equation of plane
  2965. ; Notes: This routine was much simpler, but it was too slow with objects
  2966. ;        greater than about 800 surfaces. old sort: 800=3 minutes to sort, yuk!
  2967. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2968.  
  2969. sort_them:
  2970.          cmp shouldisort,no
  2971.          je _ret
  2972.          cmp surfaces,2
  2973.          jl _ret
  2974.  
  2975.          mov edx,offset okmsg6
  2976.          call _putdosmsg
  2977.  
  2978.          call prep_num
  2979.          call prep_normal
  2980.  
  2981.          mov temp1,0
  2982.          mov temp2,1
  2983. st_inner:
  2984.          mov eax,temp1
  2985.          mov ebx,temp2
  2986.  
  2987.          call testeq
  2988.          jle st_noswitch
  2989.  
  2990.          mov eax,temp1
  2991.          mov ebx,temp2
  2992.          call switch_em
  2993.  
  2994. st_noswitch:
  2995.          inc temp2
  2996.          mov eax,surfaces
  2997.          cmp temp2,eax
  2998.          jnae st_inner
  2999.          mov eax,temp1
  3000.          inc eax
  3001.          mov temp2,eax
  3002.  
  3003.          inc temp1
  3004.          mov eax,surfaces
  3005.          dec eax
  3006.          cmp temp1,eax
  3007.          jnae st_inner
  3008.  
  3009.          call re_apply
  3010.  
  3011.          ret
  3012.  
  3013. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3014. ; Testeq: Compare normal EAX against normal EBX
  3015. ;  In:
  3016. ;  EAX = connection for first compare
  3017. ;  EBX = connection for second compare
  3018. ;  Out:
  3019. ;   jl less
  3020. ;   je equal
  3021. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3022.  
  3023. testeq:
  3024.          mov esi,connmem
  3025.          mov edx,[esi+eax*4+bv]
  3026.          mov ebp,[esi+ebx*4+bv]
  3027.  
  3028.          mov ecx,[esi+(ebp)*4+ed] ; get surface normal for inner loop
  3029.          cmp ecx,[esi+(edx)*4+ed] ; get surface normal for outer loop
  3030.          jne _ret
  3031.  
  3032.          mov ecx,[esi+(ebp)*4+ec]
  3033.          cmp ecx,[esi+(edx)*4+ec]
  3034.          jne _ret
  3035.  
  3036.          mov ecx,[esi+(ebp)*4+eb]
  3037.          cmp ecx,[esi+(edx)*4+eb]
  3038.          jne _ret
  3039.  
  3040.          mov ecx,[esi+(ebp)*4+ea]
  3041.          cmp ecx,[esi+(edx)*4+ea]
  3042.          ret
  3043.  
  3044. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3045. ; Switch_em: Switch surfaces EAX and EBX
  3046. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3047.  
  3048. switch_em:
  3049.          mov esi,connmem
  3050.          mov ecx,[esi+eax*4+sk]
  3051.          mov edi,[esi+ebx*4+sk]
  3052.          mov [esi+eax*4+sk],edi
  3053.          mov [esi+ebx*4+sk],ecx
  3054.          mov ecx,[esi+eax*4+bv]
  3055.          mov edi,[esi+ebx*4+bv]
  3056.          mov [esi+eax*4+bv],edi
  3057.          mov [esi+ebx*4+bv],ecx
  3058.          ret
  3059.  
  3060. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3061. ; Number surfaces in preparation for sort
  3062. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3063.  
  3064. prep_num:
  3065.          mov esi,connmem
  3066.          xor eax,eax
  3067.          mov ecx,surfaces
  3068. prep_loop:
  3069.          mov [esi+eax*4+sk],eax
  3070.          inc eax
  3071.          loop prep_loop
  3072.  
  3073.          ret
  3074.  
  3075. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3076. ; Prepare normal indexers ready for sort
  3077. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3078.  
  3079. prep_normal:
  3080.          mov temp1,0
  3081.          mov eax,surfaces
  3082.          mov temp2,eax
  3083. prep_lp:
  3084.          mov ebx,temp1
  3085.          call rn_lowest
  3086.          mov ebx,temp1
  3087.          mov esi,connmem
  3088.          mov [esi+ebx*4+bv],ebp
  3089.          inc temp1
  3090.          dec temp2
  3091.          jnz prep_lp
  3092.  
  3093.          ret
  3094.  
  3095. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3096. ; re_apply sort to surface list
  3097. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3098.  
  3099. re_apply:
  3100.          mov temp1,0
  3101. re_loop1:
  3102.          mov esi,connmem
  3103.          mov eax,temp1
  3104.          mov ebx,[esi+eax*4+sk]
  3105.          xchg eax,ebx
  3106.          add ebx,128000
  3107.          call rn_eax2ebx
  3108.          inc temp1
  3109.          mov eax,surfaces
  3110.          cmp temp1,eax
  3111.          jnae re_loop1
  3112.  
  3113.          mov temp1,0
  3114. re_loop2:
  3115.          mov esi,connmem
  3116.          mov eax,temp1
  3117.          mov ebx,[esi+eax*4+sk]
  3118.          mov eax,ebx
  3119.          add eax,128000
  3120.          call rn_eax2ebx
  3121.          inc temp1
  3122.          mov eax,surfaces
  3123.          cmp temp1,eax
  3124.          jnae re_loop2
  3125.  
  3126.          ret
  3127.  
  3128. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3129. ; Change any underscore characters to commas
  3130. ; In:
  3131. ;  EDX => ASCIIZ Material to scan/change
  3132. ; Out:
  3133. ;  EDX = EDX   if no change
  3134. ;  EDX = EDX+1 if no changed material
  3135. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  3136.  
  3137.  
  3138. change_underscore:
  3139.          cmp byte ptr [edx],"_"
  3140.          jne _ret
  3141.  
  3142.          inc edx      ; direct copy command used, scan for _ and replace with ,
  3143.          mov edi,edx
  3144.          mov ecx,31
  3145.          mov al,0
  3146.          repne scasb
  3147.          jcxz _ret
  3148.  
  3149.          mov edi,edx
  3150.          neg ecx
  3151.          add ecx,31
  3152.          jcxz _ret
  3153.  
  3154.          mov al,"_"
  3155. cu_morescan:
  3156.          repne scasb
  3157.          jcxz _ret
  3158.          mov byte ptr [edi-1],","
  3159.          jmp cu_morescan
  3160.  
  3161. code32   ends
  3162.          end
  3163.